PowerShell Switch Statement [With Examples]

To write a good PowerShell automation script, you should know how to use the Switch statement. I will explain everything about the PowerShell switch statement and its syntax and provide a few useful real-time examples.

Using the PowerShell switch statement, you can check multiple conditions more efficiently than using several if statements. Switch statements in PowerShell can handle different types of values, such as strings, numbers, and even regular expressions.

Let me now explain to you with some examples from the beginning.

Switch Statement in PowerShell

A switch statement in PowerShell evaluates a variable or expression against a list of possible values, executing the corresponding block of code for the matched case. This is similar to the switch statements in other programming languages like C, Java, and JavaScript. Compared to using multiple if-else statements, it provides a cleaner and more readable way to handle multiple conditions.

Syntax of PowerShell Switch Case

The basic syntax of a switch statement in PowerShell is like the below:

switch ($variable) {
    'value1' {
        # Code to execute if $variable is 'value1'
    }
    'value2' {
        # Code to execute if $variable is 'value2'
    }
    default {
        # Code to execute if no match is found
    }
}

Here’s a simple example of how you can use the switch case in PowerShell.

$day = 'Monday'

switch ($day) {
    'Monday' {
        Write-Output "Start of the work week!"
    }
    'Friday' {
        Write-Output "End of the work week!"
    }
    default {
        Write-Output "Midweek days."
    }
}

In this example, the output will be "Start of the work week!" because the value of $day is 'Monday'.

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

powershell switch statement

Read PowerShell Switch Statement with Multiple Conditions

PowerShell Switch Default Value

The default case in PowerShell Switch is used when none of the specified cases match the value.

The default keyword in a switch statement specifies the block of code that should be executed if none of the specified cases match the value of the expression.

Look at the example below.

$color = 'Yellow'

switch ($color) {
    'Red' {
        Write-Output "Stop"
    }
    'Green' {
        Write-Output "Go"
    }
    'Blue' {
        Write-Output "Cool"
    }
    default {
        Write-Output "Unknown color"
    }
}

In this case, the output will be "Unknown color" because 'Yellow' does not match any of the specified cases.

Look at the screenshot below for the output:

PowerShell Switch Default Value

Read PowerShell Switch Parameter

Use Break in a PowerShell Switch Statement

In PowerShell, the break statement can be used within a switch block to exit the switch statement prematurely. This is useful when you want to stop further evaluating cases once a match is found and the corresponding code is executed.

Below is an example where I used the break statement in a switch statement in PowerShell.

$number = 2

switch ($number) {
    1 {
        Write-Output "One"
        break
    }
    2 {
        Write-Output "Two"
        break
    }
    3 {
        Write-Output "Three"
        break
    }
    default {
        Write-Output "Other number"
    }
}

In this example, the output will be "Two" and the switch statement will terminate immediately after.

Now follow the screenshot below for the output after I executed the above PowerShell script using VS code.

powershell switch break

Read PowerShell switch wildcard examples

PowerShell Switch Case Insensitive

By default, the switch statement in PowerShell is case-insensitive. It does not distinguish between uppercase and lowercase characters when matching values. However, you can make it case-sensitive by using the -CaseSensitive parameter.

Here is a complete PowerShell script.

$fruit = 'APPLE'

switch ($fruit) {
    'apple' {
        Write-Output "This is an apple."
    }
    'banana' {
        Write-Output "This is a banana."
    }
    default {
        Write-Output "Unknown fruit."
    }
}

In this example, the output will be "This is an apple." even though the case does not match. If you want to make it case-sensitive, you can modify the switch statement as follows:

$fruit = 'APPLE'
switch -CaseSensitive ($fruit) {
    'apple' {
        Write-Output "This is an apple."
    }
    'banana' {
        Write-Output "This is a banana."
    }
    default {
        Write-Output "Unknown fruit."
    }
}

Now, the output will be "Unknown fruit." because 'APPLE' does not match 'apple' in a case-sensitive comparison.

The output is in the screenshot below. When you execute the script, it will return the same output.

powershell switch case insensitive

Read PowerShell Switch Case with Regex

Nested Switch Statement in PowerShell

Here is an example of how to use nested switch statements in PowerShell.

Example:

$data = @(
    @{Name="John"; Age=28}
    @{Name="Jane"; Age=32}
)

foreach ($person in $data) {
    switch ($person.Name) {
        "John" {
            switch ($person.Age) {
                {$_ -lt 30} { "John is under 30" }
                default { "John is 30 or older" }
            }
        }
        "Jane" {
            switch ($person.Age) {
                {$_ -lt 35} { "Jane is under 35" }
                default { "Jane is 35 or older" }
            }
        }
        default { "Unknown person" }
    }
}

PowerShell Switch Statement Examples

Now, let me show you a few practical examples of how to use the switch statement in PowerShell.

Example 1: Check File Type

This example that explains how to use the switch statement to identify the type of a file based on its extension.

Below is the complete PowerShell script.

$file = 'document.pdf'

switch -Regex ($file) {
    '\.txt$' {
        Write-Output "Text file"
    }
    '\.pdf$' {
        Write-Output "PDF document"
    }
    '\.jpg$' {
        Write-Output "JPEG image"
    }
    default {
        Write-Output "Unknown file type"
    }
}

In this example, the output will be "PDF document" because the file extension matches \.pdf$.

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

powershell switch statement examples

Example 2: User Role Assignment

This example shows how to assign user roles based on input. Look at the script below:

$userRole = 'Admin'

switch ($userRole) {
    'Admin' {
        Write-Output "Full access granted."
    }
    'Editor' {
        Write-Output "Edit access granted."
    }
    'Viewer' {
        Write-Output "Read-only access granted."
    }
    default {
        Write-Output "No access granted."
    }
}

Here, the output will be "Full access granted." because the $userRole is 'Admin'.

Example 3: Day of the Week Message

This example uses the switch statement to print messages based on the day of the week.

To get the output, you can run the below PowerShell script using VS code.

$today = (Get-Date).DayOfWeek

switch ($today) {
    'Monday' {
        Write-Output "Start your week strong!"
    }
    'Wednesday' {
        Write-Output "Midweek hustle!"
    }
    'Friday' {
        Write-Output "Almost weekend!"
    }
    'Saturday' -or 'Sunday' {
        Write-Output "Enjoy your weekend!"
    }
    default {
        Write-Output "Keep going!"
    }
}

Since today is Thursday, it is showing “Keep going’ as output. You can check the screenshot below:

powershell switch examples

Conclusion

From the above tutorial and examples, I expect you now know how to use the Switch statement in PowerShell. I have also explained how to use the break statement in the PowerShell switch case.

Feel free to leave a comment below if you still have questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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