TOPIC
about_functions_CmdletBindingAttribute
SHORT DESCRIPTION
Describes an attribute that declares a Function that acts similar to a
compiled cmdlet.
LONG DESCRIPTION
When you write Functions, you can add the CmdletBinding attribute so that
Windows PowerShell will bind the parameters of the Function in the same
way that it binds the parameters of compiled cmdlets. When this attribute
is declared, Windows PowerShell also sets the $PSCmdlet automatic Variable.
When you use cmdlet binding, unknown parameters and positional arguments
that have no matching positional parameters cause parameter binding to
fail. Also, a Function or script with cmdlet binding does not use the
$args Variable.
Note: Compiled cmdlets use the required Cmdlet attribute, which is similar
to the CmdletBinding attribute that is described in this topic.
The following example shows the outline of a Function that specifies all
the optional arguments of the CmdletBinding attribute. A brief description
of each argument follows this example.
{
[CmdletBinding(SupportsShouldProcess=<Boolean>,
ConfirmImpact=<String>,
DefaultParameterSetName=<String>)]
Param ($Parameter1)
Begin{}
Process{}
End{}
}
SupportsShouldProcess
When the SupportsShouldProcess argument is set to true, it indicates that
the Function supports calls to the ShouldProcess method, which is used to
prompt the user for feedback before the Function makes a change to the
system. When this argument is specified, the Confirm and WhatIf
parameters are enabled for the Function.
For more information about confirmation requests, see “Requesting
Confirmation” in the MSDN (Microsoft Developer Network) library at
http://go.microsoft.com/fwlink/?LinkId=136658.
DefaultParameterSetName
The DefaultParameterSetName argument specifies the name of the parameter
set that Windows PowerShell will attempt to use when it cannot determine
which parameter set to use. You can avoid this issue by making the
unique parameter of each parameter set a mandatory parameter.
ConfirmImpact
The ConfirmImpact argument specifies when the action of the Function
should be confirmed by a call to the ShouldProcess method. The call to
the ShouldProcess method displays a confirmation prompt only when the
ConfirmImpact argument is equal to or greater than the value of the
$ConfirmPreference preference Variable. (The default value of the
argument is Medium.) Specify this argument only when the
SupportsShouldProcess argument is also specified.
SEE ALSO
about_functions_advanced
about_functions_CmdletBindingAttribute
about_functions_ParameterAttributes