Parameters

Script Parameters / Arguments (for scripts, functions and script blocks)

To pass arguments to a script or cmdlet type them separated by spaces:

PS C:\> Get-ChildItem -Path $env:windir -Filter *.dll -Recurse

This can be made a little more readable by breaking up long lines with PowerShell’s escape character:
PS C:\> Get-ChildItem -Path $env:windir `
-Filter *.dll -Recurse

To pass multiple arguments to a script you can splat them:

$SS64 = @{
 Path = "$env:windir"
 filter = '*.dll
 Recurse = $true
}

Get-ChildItem @SS64

^ That will be expanded to: Get-ChildItem -Path $env:windir -Filter *.dll -Recurse

Within a script you can refer to unnamed arguments using the $args array and referrring to the position (first, second..) of each argument:

"First argument is " + $Args[0]"
"Second argument is " + $Args[1]"

To access arguments by name, use a param statement:

param($MyArg1, [int] $MyArg2 = 0)

"First argument is $MyArg1"
"Second argument is $MyArg2"

Cmdlet Parameters

Almost every powershell cmdlet can accept one or more optional parameters which can be supplied on the command line in the form -Parameter_Name Parameter_Value

The name of the parameter is always preceded by a hyphen (-)

The Parameter_value often needs to be provided in a specific data type (e.g. string or integer)
To find these details use Get-Help -detailed cmdletName

In some cases, the Parameter_Name is implied and does not need to be explicitly included.

In syntax descriptions:

 [-Param] -- is optional 
  -Param  -- is required

If you exclude the Parameter Names you must ensure that the Parameter Values are listed in the correct order (if only one value being passed you don't have to worry about this.)

Parameter Names will be ignored if contained in quotation marks.

Multiple values (for the same parameter) can be separated with commas.

“Slow down and enjoy life. It's not only the scenery you miss by going too fast, you also miss the sense of where you are going and why” - Eddie Cantor

Related:

Pipelines - Pass objects down the pipeline
Wildcards - Match multiple items



Back to the Top

© Copyright SS64.com 1999-2013
Some rights reserved