Skip to content

Latest commit

 

History

History
77 lines (60 loc) · 1.62 KB

AvoidDefaultValueSwitchParameter.md

File metadata and controls

77 lines (60 loc) · 1.62 KB
description ms.date ms.topic title
Switch Parameters Should Not Default To True
12/05/2024
reference
AvoidDefaultValueSwitchParameter

AvoidDefaultValueSwitchParameter

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.

You shouldn't define a switch parameter with a default value of $true because this isn't the expected behavior of a switch parameter.

How

Change the default value of the switch parameter to be $false or don't provide a default value. Write the logic of the script to assume that the switch parameter default value is $false or not provided.

Example

Wrong

function Test-Script
{
    [CmdletBinding()]
    Param
    (
        [String]
        $Param1,

        [switch]
        $Switch=$True
    )
    ...
}

Correct

function Test-Script
{
    [CmdletBinding()]
    Param
    (
        [String]
        $Param1,

        [switch]
        $Switch
    )

    begin {
        # Ensure that the $Switch is set to false if not provided
        if (-not $PSBoundParameters.ContainsKey('Switch')) {
            $Switch = $false
        }
    }
    ...
}

More information