Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Severity Level: Warning
Description
If your parameter takes only true and false, define the parameter as type [Switch]. PowerShell
treats a switch parameter as true when it's used with a command. If the parameter isn't included
with the command, PowerShell considers the parameter to be false. Don't define [Boolean]
parameters.
Don't define a switch parameter with a default value of $true because a switch parameter is
already $false when it isn't specified. Leave the switch parameter without a default value so it
behaves as designed.
How
To fix this issue, don't assign a default value of $true to a [switch] parameter. Declare the
switch without a default value and write your logic so the parameter is treated as $false when
the caller doesn't supply it.
More information
See Strongly Encouraged Development Guidelines.
Example
Noncompliant
function Test-Script
{
[CmdletBinding()]
Param
(
[String]
$Param1,
[switch]
$Switch=$True
)
...
}
Preferred
function Test-Script
{
[CmdletBinding()]
Param
(
[String]
$Param1,
[switch]
$Switch
)
...
}