Skip to content

no-default-true-switch

PowerShellsuspiciousno fixdefault: warnpowershell/psscriptanalyzer

Reports a [switch] parameter with the default value $true.

A switch is present or absent, and the caller turns it on by naming it. A switch that is on by default can be turned off only with the odd form -Name:$false.

To fix, invert the meaning, with a name for the opposite (-NoLog for -Log).

Reported
...
[securestring]$Secret,
[string]$Name,
[switch]$Force = $true,
[switch]$Quiet
)
...
powershell:
lint:
rules:
no-default-true-switch: warn # off, warn (default), error

Enabled by the presets: powershell/psscriptanalyzer.

Suppress one line with sudsy-disable-next-line no-default-true-switch in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
psscriptanalyzer PSAvoidDefaultValueSwitchParameter partial Other values that evaluate to true (= 1) are not reported.