I am looking for the best way to call a function in powershell.
I search on google and find the two below ways to call a function. Ref - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1
Note - Assume function with name 'func' and 2 params ($Size, $Length)
is there any different way other than mentioned above to call a function in PowerShell ?
As commented by @Santiago:
Both ways are valid, there is no "better way" except for named parameters being more verbose could help others get a better understanding of what your function is doing
The best practice (for this particular question) is actually included in PSScriptAnalyzer that comes standard with e.g. the PowerShell Extension for Visual Studio Code and gives you (best practice) warnings on the fly.
You might also install PSScriptAnalyzer
and check your scripts with Invoke-ScriptAnalyzer
.
The specific rule that applies to your question can be checked with Get-ScriptAnalyzerRule
:
(Get-ScriptAnalyzerRule PSAvoidUsingPositionalParameters).Description
Readability and clarity should be the goal of any script we expect to maintain over time. When calling a command that takes parameters, where possible consider using name parameters as opposed to positional parameters. To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.