11---
22description : Explains how to add parameters to advanced functions.
33Locale : en-US
4- ms.date : 01/06 /2025
4+ ms.date : 02/25 /2025
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about_Functions_Advanced_Parameters
@@ -36,8 +36,8 @@ combination with the `[Alias()]` attribute or any of the parameter validation
3636attributes.
3737
3838Parameter names follow the rules for variable names. Parameter names consist of
39- decimal digits, alphabetic characters, and underscores. For a complete list of
40- naming rules, see [ about_Variables] [ 20 ] .
39+ decimal digits, alphabetical characters, and underscores. For a complete list
40+ of naming rules, see [ about_Variables] [ 20 ] .
4141
4242> [ !IMPORTANT]
4343> It's possible to define a parameter that starts with a decimal digit.
@@ -189,24 +189,23 @@ has a **false** value.
189189For example, the ** Recurse** parameter of ` Get-ChildItem ` is a switch
190190parameter.
191191
192- To create a switch parameter in a function, specify the ` switch ` type in the
193- parameter definition.
194-
195- For example, your function may have an option to output data as a byte array:
192+ To create a switch parameter in a function, specify the ` [ switch] ` type in the
193+ parameter definition. The following example shows the definition of a switch
194+ parameter that could be used to provide an option to output data as a byte
195+ array:
196196
197197``` powershell
198198param([switch]$AsByteArray)
199199```
200200
201- Switch parameters are easy to use and are preferred over Boolean parameters,
202- which have a less natural syntax for PowerShell.
201+ Switch parameters are easy to use and are preferred over Boolean parameters
202+ that have a less natural syntax for PowerShell.
203203
204- For example, to use a switch parameter, the user types the parameter in the
205- command.
204+ To use a switch parameter, include the parameter in the command. For example:
206205
207206` -IncludeAll `
208207
209- To use a Boolean parameter, the user types the parameter and a Boolean value.
208+ To use a Boolean parameter, you must provide the parameter and a Boolean value.
210209
211210` -IncludeAll $true `
212211
@@ -217,29 +216,37 @@ value is required.
217216
218217### Switch parameter design considerations
219218
220- - Switch parameters shouldn 't be given default values. They should always
219+ - Don 't set a default value for a switch parameter. Switch parameter always
221220 default to false.
222- - Switch parameters are excluded from positional parameters by default. Even
223- when other parameters are implicitly positional, switch parameters aren't.
224- You _ can_ override that in the Parameter attribute, but it will confuse
225- users.
226- - Switch parameters should be designed so that setting them moves a command
227- from its default behavior to a less common or more complicated mode. The
228- simplest behavior of a command should be the default behavior that doesn't
229- require the use of switch parameters.
230- - Switch parameters shouldn't be mandatory. The only case where it's necessary
231- to make a switch parameter mandatory is when it's needed to differentiate a
221+ - Don't make switch parameters positional. By default, switch parameters are
222+ excluded from positional parameters. You _ can_ override that in the
223+ ** Parameter** attribute, but it can confuse users.
224+ - Design switch parameters so that using parameter changes the default behavior
225+ of the command to a less common or more complicated mode. The simplest
226+ behavior of a command should be the default behavior that doesn't require the
227+ use of switch parameters.
228+ - Don't make switch parameters mandatory. The only case where it's helpful to
229+ make a switch parameter mandatory is when it's needed to differentiate a
232230 parameter set.
233- - Explicitly setting a switch from a boolean can be done with
234- ` -MySwitch:$boolValue ` and in splatting with
235- ` $params = @{ MySwitch = $boolValue } ` .
236- - Switch parameters are of type ` SwitchParameter ` , which implicitly converts to
237- Boolean. The parameter variable can be used directly in a conditional
238- expression. For example:
231+ - Use the switch parameter variable directly in a conditional expression. The
232+ ` SwitchParameter ` type implicitly converts to Boolean. For example:
233+
234+ ``` powershell
235+ if ($MySwitch) { ... }
236+ ```
237+
238+ - Always base the behavior controlled by the switch on the value of the switch,
239+ not the presence of the parameter. There are several ways to test for the
240+ presence of a switch parameters:
239241
240- ` if ($MySwitch) { ... } `
242+ - ` $PSBoundParameters ` contains the switch parameter name as a key
243+ - ` $MyInvocation.BoundParameters ` contains the switch parameter name as a key
244+ - ` $PSCmdlet.ParameterSetName ` when the switch defines a unique parameter set
241245
242- There's no need to write ` if ($MySwitch.IsPresent) { ... } `
246+ For example, it's possible to provide an explicit value for the switch using
247+ ` -MySwitch:$false ` or splatting. If you only test for the presence of the
248+ parameter, the command behaves as if the switch value is ` $true ` instead of
249+ ` $false ` .
243250
244251## Dynamic parameters
245252
@@ -594,7 +601,7 @@ function Test-Remainder {
594601 "${i}: $($Remaining[$i])"
595602 }
596603}
597- Test-Remainder first one,two
604+ Test-Remainder first one, two
598605```
599606
600607``` Output
0 commit comments