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
Default state: Always enabled
Description
This rule detects switch parameters that are assigned a default value of $true. Switch parameters
shouldn't have default values. By design, a switch parameter is $false when not specified and
$true when included in the command. Assigning a default value of $true to a switch parameter
violates this design principle and can cause unexpected behavior.
If your parameter needs to accept only true and false values, use the [Switch] type instead of
[Boolean]. PowerShell automatically handles switch parameters correctly without requiring a
default value.
To fix this issue, remove the default value from the switch parameter declaration. The switch
naturally defaults to $false when not specified, allowing your logic to respond appropriately to
the caller's input.
To learn more, see Strongly Encouraged Development Guidelines.
Example
Noncompliant
function Test-Script
{
[CmdletBinding()]
Param
(
[String]
$Param1,
[switch]
$Switch=$True
)
...
}
Compliant
function Test-Script
{
[CmdletBinding()]
Param
(
[String]
$Param1,
[switch]
$Switch
)
...
}
Configure rule
This rule is always enabled and isn't configurable. Use one of the following methods to avoid using this rule:
- Create a custom rule configuration file to include only the rules you want or exclude the rules you don't want.
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific code blocks. For more information, see the Suppressing rules section of Using PSScriptAnalyzer.