Help About - Functions - Advanced - Parameters
Help About - Functions - Advanced - Parameters
about_Functions_Advanced_Parameters
SHORT DESCRIPTION
Explains how to add parameters to advanced functions.
LONG DESCRIPTION
You can add parameters to the advanced functions that you write, and use
parameter attributes and arguments to limit the parameter values that
function users submit with the parameter.
The parameters that you add to your function are available to users in
addition to the common parameters that Windows PowerShell adds automatically
to all cmdlets and advanced functions. For more information about the Windows
PowerShell common parameters, see about_CommonParameters
(http://go.microsoft.com/fwlink/?LinkID=113216).
Beginning in Windows PowerShell 3.0, you can use splatting with @Args to
represent the parameters in a command. This technique is valid on simple
and advanced functions. For more information, see about_Functions
(http://go.microsoft.com/fwlink/?LinkID=113231) and about_Splatting
(http://go.microsoft.com/fwlink/?LinkID=262720).
Static Parameters
Static parameters are parameters that are always available in the function.
Most parameters in Windows PowerShell cmdlets and scripts are static
parameters.
- It is mandatory (required).
- It takes input from the pipeline.
- It takes an array of strings as input.
Param
(
[parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[String[]]
$ComputerName
)
Attributes of Parameters
This section describes the attributes that you can add to function
parameters.
You can add one or multiple attributes in each parameter declaration. There
is no limit to the number of attributes that you can add to a parameter
declaration.
The Parameter Attribute
The Parameter attribute is optional, and you can omit it if none of the
parameters of your functions need attributes, but to be recognized as
an advanced function (rather than a simple function), a function must have
either the CmdletBinding attribute or the Parameter attribute, or both.
Param
(
[parameter(Argument=value)]
$ParameterName
)
Param
(
[parameter(Argument1=value1,
Argument2=value2)]
Param
(
[parameter()]
$ParameterName
)
Mandatory Argument
Param
(
[parameter(Mandatory=$true)]
[String[]]
$ComputerName
)
Position Argument
Param
(
[parameter(Position=0)]
[String[]]
$ComputerName
)
ParameterSetName Argument
The ParameterSetName argument specifies the parameter set to which a
parameter belongs. If no parameter set is specified, the parameter
belongs to all the parameter sets defined by the function. Therefore, to
be unique, each parameter set must have at least one parameter that is
not a member of any other parameter set.
Param
(
[parameter(Mandatory=$true,
ParameterSetName="Computer")]
[String[]]
$ComputerName,
[parameter(Mandatory=$true,
ParameterSetName="User")]
[String[]]
$UserName,
[parameter(Mandatory=$false)]
[Switch]
$Summary
)
You can specify only one ParameterSetName value in each argument and only
one ParameterSetName argument in each Parameter attribute. To indicate that
a parameter appears in more than one parameter set, add additional
Parameter
attributes.
The following example explicitly adds the Summary parameter to the Computer
and User parameter sets. The Summary parameter is mandatory in one
parameter
set and optional in the other.
Param
(
[parameter(Mandatory=$true,
ParameterSetName="Computer")]
[String[]]
$ComputerName,
[parameter(Mandatory=$true,
ParameterSetName="User")]
[String[]]
$UserName,
[parameter(Mandatory=$false, ParameterSetName="Computer")]
[parameter(Mandatory=$true, ParameterSetName="User")]
[Switch]
$Summary
)
For more information about parameter sets, see "Cmdlet Parameter Sets"
in the MSDN library at http://go.microsoft.com/fwlink/?LinkId=142183.
ValueFromPipeline Argument
Param
(
[parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[String[]]
$ComputerName
)
ValueFromPipelineByPropertyName Argument
Param
(
[parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]
$ComputerName
)
ValueFromRemainingArguments Argument
Param
(
[parameter(Mandatory=$true,
ValueFromRemainingArguments=$true)]
[String[]]
$ComputerName
)
HelpMessage Argument
Param
(
[parameter(mandatory=$true,
HelpMessage="Enter one or more computer names separated by
commas.")]
[String[]]
$ComputerName
)
Alias Attribute
The Alias attribute establishes an alternate name for the parameter. There
is no limit to the number of aliases that you can assign to a parameter.
The following example shows a parameter declaration that adds the "CN" and
"MachineName" aliases to the mandatory ComputerName parameter.
Param
(
[parameter(Mandatory=$true)]
[alias("CN","MachineName")]
[String[]]
$ComputerName
)
Param
(
[parameter(Mandatory=$true)]
[AllowNull()]
[String]
$ComputerName
)
Param
(
[parameter(Mandatory=$true)]
[AllowEmptyString()]
[String]
$ComputerName
)
Param
(
[parameter(Mandatory=$true)]
[AllowEmptyCollection()]
[String[]]
$ComputerName
)
Param
(
[parameter(Mandatory=$true)]
[ValidateCount(1,5)]
[String[]]
$ComputerName
)
Param
(
[parameter(Mandatory=$true)]
[ValidateLength(1,10)]
[String[]]
$ComputerName
)
[Int32][ValidateLength(1,10)]$number = 01
Param
(
[parameter(Mandatory=$true)]
[ValidatePattern("[0-9][0-9][0-9][0-9]")]
[String[]]
$ComputerName
)
[Int32][ValidatePattern("[0-9][0-9][0-9][0-9]")]$number = 1111
Param
(
[parameter(Mandatory=$true)]
[ValidateRange(0,10)]
[Int]
$Attempts
)
[Int32][ValidateRange(0,10)]$number = 5
Param
(
[parameter()]
[ValidateScript({$_ -ge (get-date)})]
[DateTime]
$EventDate
)
ValidateSet Attribute
Param
(
[parameter(Mandatory=$true)]
[ValidateSet("Low", "Average", "High")]
[String[]]
$Detail
)
Param
(
[parameter(Mandatory=$true)]
[ValidateNotNull()]
$ID
)
Param
(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]]
$UserName
)
Dynamic Parameters
You can also create a parameter that appears only when another parameter
is used in the function command or when another parameter has a certain
value.
Dynamic parameters can be very useful, but use them only when necessary,
because they can be difficult for users to discover. To find a dynamic
parameter, the user must be in the provider path, use the ArgumentList
parameter of the Get-Command cmdlet, or use the Path parameter of Get-Help.
DynamicParam {<statement-list>}
function Get-Sample {
[CmdletBinding()]
Param ([String]$Name, [String]$Path)
DynamicParam
{
if ($path -match ".*HKLM.*:")
{
$attributes = new-object
System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "__AllParameterSets"
$attributes.Mandatory = $false
$attributeCollection = new-object `
-Type
System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
$dynParam1 = new-object `
-Type
System.Management.Automation.RuntimeDefinedParameter("dp1", [Int32],
$attributeCollection)
$paramDictionary = new-object `
-Type
System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("dp1", $dynParam1)
return $paramDictionary
}
}
}
Switch Parameters
Switch parameters are parameters with no parameter value. They
are effective only when they are used and have only one effect.
For example:
Param ([Switch]<ParameterName>)
-or-
Param
(
[parameter(Mandatory=$false)]
[Switch]
$<ParameterName>
)
Switch parameters are easy to use and are preferred over Boolean
parameters, which have a more difficult syntax.
For example, to use a switch parameter, the user types the parameter
in the command.
-IncludeAll
To use a Boolean parameter, the user types the parameter and a Boolean
value.
-IncludeAll:$true
SEE ALSO
about_Functions
about_Functions_Advanced
about_Functions_Advanced_Methods
about_Functions_CmdletBindingAttribute
about_Functions_OutputTypeAttribute