PowerShell Switch Statement with Multiple Conditions

Recently, while working on a PowerShell script, one of my team members asked how to use multiple conditions in a PowerShell switch statement. I tried the solution and will explain the PowerShell switch statement with multiple conditions, with three useful examples.

Note: I have executed all the PowerShell scripts using the VS code editor.

Switch Statement in PowerShell

A switch statement in PowerShell is similar to the switch or case statements found in other programming languages. It provides a cleaner and more readable way to handle multiple conditions compared to using multiple if statements.

Here’s a basic example of a switch statement:

$a = 3
switch ($a) {
    1 { "It is one." }
    2 { "It is two." }
    3 { "It is three." }
    default { "It is something else." }
}

In this example, the variable $a is evaluated against the values 1, 2, and 3. Since $a is 3, the output will be “It is three.”

I have also written a complete tutorial on how to use switch case in PowerShell, you can go through it.

Now, let us focus on how to use multiple conditions in the switch statement in PowerShell.

Switch Statement with Multiple Conditions in PowerShell

PowerShell allows you to test multiple conditions within a single switch statement case. This is done by using the $_ variable and logical operators within script blocks.

Let me show you three realtime examples to understand it better.

Example 1: Multiple Values in a Single Case

You can specify multiple values in a single case by using the $_ variable and logical operators.

Here is an example and the complete PowerShell script.

$day = "Saturday"
switch ($day) {
    {$_ -eq "Saturday" -or $_ -eq "Sunday"} { "It is the weekend." }
    {$_ -eq "Monday" -or $_ -eq "Tuesday" -or $_ -eq "Wednesday" -or $_ -eq "Thursday" -or $_ -eq "Friday"} { "It is a weekday." }
    default { "Unknown day." }
}

In this example, if $day is either “Saturday” or “Sunday”, the output will be “It is the weekend.” For any weekday, the output will be “It is a weekday.”

After I executed the above PowerShell script, it gave me the exact output; look at the screenshot below:

powershell switch case multiple values

Read PowerShell Switch Parameter

Example 2: Use Multiple Conditions with Logical Operators

Sometimes, you might need to evaluate more complex conditions using logical operators. While the switch statement itself doesn’t support logical operators directly within the case labels, but you can use script blocks to achieve this.

Here is an example of using the logical operators in the Switch statement in PowerShell.

$number = 8
switch ($number) {
    { $_ -lt 5 } { "The number is less than 5." }
    { $_ -ge 5 -and $_ -le 10 } { "The number is between 5 and 10." }
    { $_ -gt 10 } { "The number is greater than 10." }
    default { "Unknown number." }
}

In this example, the variable $number is checked against multiple conditions using script blocks. The $_ represents the current item being evaluated. If $number is 8, the output will be “The number is between 5 and 10.”

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

PowerShell Switch Statement with Multiple Conditions

Read PowerShell Switch Case with Regex

Example 3: Combine Conditions and Actions

You can also combine conditions and actions within a switch statement to handle more complex scenarios in PowerShell.

Look at the example and the script below:

$input = "apple"
switch ($input) {
    {$_ -eq "apple" -or $_ -eq "banana"} { "It's a fruit." }
    {$_ -eq "carrot" -or $_ -eq "lettuce"} { "It's a vegetable." }
    { $_ -match "berry" } { "It's a type of berry." }
    default { "Unknown item." }
}

In this example, the switch statement handles different types of input. If $input is “apple” or “banana”, the output will be “It’s a fruit.” If it matches a pattern containing “berry”, the output will be “It’s a type of berry.”

Here is the output in the screenshot below:

Multiple Conditions in PowerShell Switch Statement

I hope you learned how to use multiple conditions in a PowerShell switch statement with these useful examples.

You may also like the following PowerShell 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.