PowerShell Switch Parameter [With Examples]

In PowerShell, a switch parameter is a special type of parameter used in functions and scripts to represent a Boolean option. A switch parameter can either be present (true) or absent (false), and it does not require an explicit value to be passed. I will explain here, everything about the PowerShell Switch Parameter with various examples.

PowerShell Switch Parameter

To define a switch parameter in a function in PowerShell, you use the [switch] attribute in the parameter declaration. Here’s an example:

function Test-Switch {
    param (
        [switch]$EnableFeature
    )

    if ($EnableFeature) {
        Write-Output "Feature is enabled"
    } else {
        Write-Output "Feature is disabled"
    }
}

Test-Switch
Test-Switch -EnableFeature

In this example, the -EnableFeature switch parameter is defined. When the function is called without the -EnableFeature parameter, it defaults to false. When the -EnableFeature parameter is included, it is set to true.

I executed the above PowerShell script, and you can see the output in the screenshot below:

PowerShell Switch Parameter

PowerShell Switch Parameter Default Value

Switch parameters in PowerShell can have default values. By default, a switch parameter is false unless explicitly set to true. This is useful for creating optional parameters in a PowerShell function.

Here is an example of how to set the default value in the PowerShell Switch Parameter.

function Test-Switch {
    param (
        [switch]$EnableFeature
    )

    if ($EnableFeature) {
        Write-Output "Feature is enabled"
    } else {
        Write-Output "Feature is disabled"
    }
}

Test-Switch
Test-Switch -EnableFeature

In this example, the -EnableFeature switch parameter is false by default. When -EnableFeature is specified, it becomes true.

You can see the exact output in the screenshot below:

PowerShell Switch Parameter Default Value

Read PowerShell Switch Case with Regex

PowerShell Switch Parameter Default True

Now, let me show you another example where we can set the switch parameter default value to true in PowerShell.

Although switch parameters default to false, you might want to set a switch parameter to true by default. This can be done by setting the parameter’s default value in the function definition.

Here is an example and the complete PowerShell script.

function Test-Switch {
    param (
        [switch]$EnableFeature = $true
    )

    if ($EnableFeature) {
        Write-Output "Feature is enabled"
    } else {
        Write-Output "Feature is disabled"
    }
}

Test-Switch
Test-Switch -EnableFeature:$false

In this example, the -EnableFeature switch parameter is true by default. You can explicitly set it to false by using -EnableFeature:$false.

After I execute the above PowerShell script, the output will appear as in the screenshot below.

PowerShell Switch Parameter Default True

Check out PowerShell Switch String Contains

PowerShell Switch Parameter Boolean

Switch parameters are essentially Boolean values. They are either true or false. This makes them ideal for enabling or disabling features within your scripts.

Here is a complete example.

function Test-Boolean {
    param (
        [switch]$IsTrue
    )

    if ($IsTrue) {
        Write-Output "The parameter is true."
    } else {
        Write-Output "The parameter is false."
    }
}

Test-Boolean
Test-Boolean -IsTrue

In this example, the -IsTrue switch parameter is a Boolean value that controls the function’s output.

Here is the output in the screenshot below after I executed the above PowerShell script.

PowerShell Switch Parameter Boolean

PowerShell Switch Parameter Examples

Let me show you a few Switch parameter examples in PowerShell.

Example 1: Enable Verbose Output

Verbose output provides additional details during script execution, which can help debug or understand what’s happening.

Here is the complete script.

function Get-Data {
    param (
        [switch]$Verbose
    )

    Write-Output "Fetching data..."
    if ($Verbose) {
        Write-Output "Verbose: Connecting to the database..."
        Write-Output "Verbose: Executing query..."
    }
    Write-Output "Data fetched successfully."
}

# Calling the function without the switch
Get-Data

# Calling the function with the switch
Get-Data -Verbose
  • Without the -Verbose switch: The function outputs only the basic messages.
  • With the -Verbose switch: Additional detailed messages are output, providing more insight into the performed steps.

Example 2: Enable Logging

Logging is very important in PowerShell, especially in production environments. This example shows how to enable or disable logging using a switch parameter.

Here is an example and a complete script.

function Start-ProcessWithLogging {
    param (
        [switch]$EnableLogging
    )

    if ($EnableLogging) {
        Write-Output "Logging is enabled."
    } else {
        Write-Output "Logging is disabled."
    }

    # Simulate a process
    Write-Output "Process started..."
    if ($EnableLogging) {
        Write-Output "Logging: Process is running."
    }
    Write-Output "Process completed."
}

# Calling the function without the switch
Start-ProcessWithLogging

# Calling the function with the switch
Start-ProcessWithLogging -EnableLogging
  • Without the -EnableLogging switch: Logging messages are not displayed.
  • With the -EnableLogging switch: Logging messages are displayed, showing the progress of the process.

Example 3: Toggle a Feature

Here is another example of using a switch parameter to toggle a specific feature within a PowerShell script.

Here is the complete script.

function Toggle-Feature {
    param (
        [switch]$FeatureEnabled
    )

    if ($FeatureEnabled) {
        Write-Output "The feature is enabled."
    } else {
        Write-Output "The feature is disabled."
    }
}

# Calling the function without the switch
Toggle-Feature

# Calling the function with the switch
Toggle-Feature -FeatureEnabled
  • Without the -FeatureEnabled switch: The feature is considered disabled.
  • With the -FeatureEnabled switch: The feature is enabled, and the corresponding message is displayed.

I hope you know how to use the PowerShell switch parameter with various examples. The concept is not easy, but I hope you understand it from the above examples.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.