Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function parameters in PowerShell

Is there any reason to use the "Param( ... )" construction inside a function definition?

My understanding is that you should use this for specifying parameters to scripts (and scriptblocks). I see a lot of examples on the web with parameters listed this way instead of just listing the parameters after the function name.

Example:

function Foo([string]$Bar){
   #Body of function
}

Or,

function Foo{
     Param([string]$Bar)

}
like image 673
Mike Shepard Avatar asked May 15 '09 18:05

Mike Shepard


People also ask

How do I add a parameter to a PowerShell function?

When developing your PowerShell functions, sometimes you need to put some input values to your functions, such as a file name, string, or any other value. In PowerShell, there are two ways to pass parameters to functions: through the $Args variable and by setting formal parameters.

What are parameters in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.

What are the parameters of the function?

Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.

How do I show parameters in PowerShell?

Simply type Get-ChildItem. Then type – and then press CTRL + Space. This shows all parameters and afterwards you can use the arrow keys to choose one.


3 Answers

In V1, it really is just a preference. Using the param statement has traditionally been more formal, but functionally it's the same thing.

You will probably see a lot more use of PARAM in PowerShell V2 because of the ability to add attributes to parameters, similar to attributes in C#.

param
(
[Parameter( Mandatory=$true,
            Position=0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
[Alias("name")]
[String]
$path
)

Here you can specify the parameter attribute and a bunch of arguments to that attribute. You can also specify aliases and a number of other validation requiremets.

Keith makes a couple of great points as well. You can use the param statement to pass parameters to annonymous scriptblocks (lambda expressions) or to a PS1 script.

like image 58
Andy Schneider Avatar answered Sep 22 '22 13:09

Andy Schneider


param() is required to specify the parameters for a script file and for scriptblocks:

PS> $sb = {param($x,$y) $x + $y}
PS> &$sb 1 3
4

Having it also be available for traditional functions is just a consistency thing IMO. It is nicer for advanced functions which tend to have a lot of baggage, er metadata, per parameter.

like image 29
Keith Hill Avatar answered Sep 22 '22 13:09

Keith Hill


Possibly as a consistency measure? Always specifying parameters the same way improves readability.

like image 30
Orihara Avatar answered Sep 25 '22 13:09

Orihara