---
title: "PowerShell Switch Statement: A Comprehensive Guide"
description: "Master the versatile PowerShell switch statement and its various use cases. Enhance your scripting skills and optimize your IT workflow."
canonical: "https://adamtheautomator.com/powershell-switch/"
---

# PowerShell Switch Statement: A Comprehensive Guide

> Master the versatile PowerShell switch statement and its various use cases. Enhance your scripting skills and optimize your IT workflow.

Source: https://adamtheautomator.com/powershell-switch/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Switch Statement: A Comprehensive Guide](https://adamtheautomator.com/wp-content/uploads/2020/03/lukas-k-03G6xINyvwA-unsplash.jpg)

# PowerShell Switch Statement: A Comprehensive Guide

[![](https://secure.gravatar.com/avatar/9a14f10ff1b1ec7d790d34f5b559e4d3de2d31b172e6ef266dfd8b479174d97b?s=192&d=mm&r=g)June Castillote](https://adamtheautomator.com/author/june/)25 March 202010 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Understanding the Basic PowerShell Switch Statement](#understanding-the-basic-powershell-switch-statement)
*   [Defining a Default Match Clause](#defining-a-default-match-clause)
*   [Evaluating Multiple Test Values](#evaluating-multiple-test-values)
*   [Matching Multiple Conditions](#matching-multiple-conditions)
*   [Terminating a Powershell Switch with the Break Statement](#terminating-a-switch-with-the-break-statement)
*   [Exploring More of the Powershell Switch Statement](#exploring-more-of-the-switch-statement)
*   [Different Switch Statement Parameters](#different-switch-statement-parameters)
*   [Full Switch Statement Syntax](#full-switch-statement-syntax)
*   [Using the -File Parameter](#using-the-file-parameter)
*   [Using the -Wildcard Parameter](#using-the-wildcard-parameter)
*   [Using the -CaseSensitive Parameter](#using-the-casesensitive-parameter)
*   [Using the -RegEx Parameter](#using-the-regex-parameter)
*   [Conclusion](#conclusion)
*   [Further Reading](#further-reading)

The concept of conditional logic in programming is not anything new. Conditional logic is a fancy term for first checking for a condition (if something happened) and then doing something as a result of that condition.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

For example, in a script that creates a report of all disk space usage, conditional logic is useful for evaluating the free space status. Based on a defined threshold, the free disk space status may be tagged as _normal_, _warning_, or _critical_ when evaluated using conditional logic.

Most, if not all, languages use their own version of the conditional logic construct. Think of if/then statement. One of the conditional logic constructs available in PowerShell is the s_witch statement_.

In this article, you will learn what the PowerShell switch statement is, understand its syntax and how it works. You will also learn from the PowerShell switch examples some of the different ways that you can use conditional logic handling in your scripts.

## Understanding the Basic PowerShell Switch Statement

The PowerShell switch statement is a conditional logic statement used for evaluating one or more conditions. It is comparable to the PowerShell _[If statement](https://adamtheautomator.com/powershell-if-statement/)._ They are both used for the same purpose – to test conditions before running actions. Why use the switch statement over if statements? When you must check multiple conditions.

Using an if statement for multiple conditions can be tedious and lengthy. Evaluating multiple conditions is easier with the use of switch statements instead. Please refer to the switch statement syntax below.

```powershell
Switch (<test-value>)
{
    <condition> {<action>}
    <condition> {<action>}
}
```

As you can see from the syntax above, the switch statement starts with  `Switch` and the value to test is enclosed in parenthesis `()`. Then, inside the curly brackets `{}` are the conditions or case, and actions list.

A `condition` or `case` can be a value or an expression. An `action` can also be a value to return, or an expression to run specified actions.

The code below prompts the user to input a number. That number is stored in the `$num` variable. Then, the value of `$num` is evaluated by the conditions and then runs the associated action when the test value and the condition matched.

```powershell
$num = Read-Host "Enter a number"
Switch ($num)
{
    1 {'Run Action 1'}
    2 {'Run Action 2'}
    3 {'Run Action 3'}
}
```

When you run the code above in PowerShell, the expected output is shown below.

![Using switch to test number input](/wp-content/uploads/2020/03/switch01.gif)

Using switch to test number input

So what happened? The value of the `$num` variable matched a PowerShell switch case from the list of conditions ( `3` ), whose action is to output _`Run Action 3`_ on the screen.

### Defining a Default Match Clause

The switch statement below only have three conditions. Those three conditions evaluate if the value of the `$num` variable is 1, 2 or 3.

```powershell
$num = Read-Host "Enter a number"
Switch ($num)
{
    1 {'Run Action 1'}
    2 {'Run Action 2'}
    3 {'Run Action 3'}
}
```

How about if the value of `$num` does not match any of the conditions? In that situation, when the test value does not match any of the conditions, the code will do nothing.

See the example below when the code is run in PowerShell, and the user enters the number 4. The switch statement ends and returns nothing. That is because there is no condition to evaluate the input value of 4.

![Switch test returns nothing](/wp-content/uploads/2020/03/switch02.gif)

Switch test returns nothing

Instead of returning nothing, it would be nice if the switch statement runs a default action for when no condition is matched. That’s where the `default` match clause comes into play.

See the modified example code below. A `default` match clause is added at the end to display a default message if the input test value does not match any of the conditions. `Default` is the equivalent of the `Else` clause in the `If` statement.

```powershell
$num = Read-Host "Enter a number"
Switch ($num)
{
    1 {'Run Action 1'}
    2 {'Run Action 2'}
    3 {'Run Action 3'}
    default {'No Action'}
}
```

And when the modified code is run in PowerShell, the expected output is shown below.

![Switch statement with a default match clause](/wp-content/uploads/2020/03/switch03.gif)

Switch statement with a default match clause

### Evaluating Multiple Test Values

So far, you’ve seen how the PowerShell switch statement is used to evaluate a single test value. But, the switch statement can also test multiple test values against a list of conditions.

In this next example, the code prompts the user to enter two numbers. The switch statement then tests the two values, one at a time according to their ordinal places.

```powershell
$num1 = Read-Host "Enter first number"
$num2 = Read-Host "Enter second number"
switch ($num1, $num2)
{
    1 {"It is one." }
    2 {"It is two." }
    3 {"It is three." }
    4 {"It is four." }
    default {"Not found"}
}
```

![Switch with multiple test values](/wp-content/uploads/2020/03/switch04.gif)

Switch with multiple test values

You can also pass an array as the test value for the switch statement like you see from the example below.

```powershell
$arrayNum = @(1,3)
switch ($arrayNum)
{
    1 {"It is one." }
    2 {"It is two." }
    3 {"It is three." }
    4 {"It is four." }
    default {"Not found"}
}
```

### Matching Multiple Conditions

Unlike the If statement where the statement exits when the first match is found, the switch statement will test all conditions even after the first match is found. This makes it possible for the switch statement to match the test value with multiple conditions.

In this example below, the test value `banana` matches multiple conditions inside the switch statement.

```powershell
switch ("banana") {
    "apple" { "I have an apple" }
    "orange" { "I have an orange" }
    "banana" { "I have a banana" }
    "mango" { "I have a mango" }
    "banana" { "I have a banana, again!" }
    default { "I don't have that fruit" }
}
```

The output below shows that the test value `banana` matched two conditions that triggered the corresponding actions.

![Switch statement matches multiple conditions](/wp-content/uploads/2020/03/switch05.gif)

Switch statement matches multiple conditions

### Terminating a Powershell Switch with the Break Statement

In the previous section, you’ve seen how the test value can match multiple conditions in a switch statement. Depending on the purpose of your script, this behavior of matching more than one condition may be OK.

But, you can have more control of the script if you can instruct the switch statement to break its flow when the test value already found its first match. This is where the _[break statement](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_break?view=powershell-7.2)_ is useful.

In this example, the same code in the previous section is used but modified to include the break statement. As you can see, each condition is followed by the `break` statement, which causes the switch statement to terminate once the test value matched a condition.

```powershell
switch ("banana") {
    "apple" { "I have an apple"; break }
    "orange" { "I have an orange"; break }
    "banana" { "I have a banana"; break }
    "mango" { "I have a mango"; break }
    "banana" { "I have a banana, again!"; break }
    default { "I don't have that fruit" }
}
```

When you put the code above in action, the below output is what it would look like. Although two conditions that evaluate the test value “banana”, only the result of the first matching condition is returned.

![Switch statement terminated with the break statement](/wp-content/uploads/2020/03/switch06.gif)

Switch statement terminated with the break statement

## Exploring More of the Powershell Switch Statement

The previous sections showed you the bare essentials about the switch statement. But the switch statement is more than just the basics. At times you may need to do more with it than just matching simple strings and numerical values. And in the next sections, you’ll learn about the different parameters or options available with the switch statement.

### Different Switch Statement Parameters

There are five parameters to the switch statement. These parameters are:

*   **_exact_** – This applies to _string_ test values only. If this parameter is used, it indicates that the condition will only apply if the statement exactly matches the test value. On the other hand, if this parameter is not used, the switch statement behaves as though the `exact` parameter is used.
*   **_casesensitive_** – This parameter is for string test values. Using this parameter indicates that the switch statement will perform a case sensitive match.
*   **_regex_** – Using this parameter indicates that the switch will perform a regular expression matching. This statement only applies to _string_ test values.
*   **_wildcard_** – This parameter also only applies to _string_ test values. Using this parameter indicates that the switch statement will accommodate testing for wildcard string. The comparison to be performed will be case-insensitive.
*   **_file_** – This parameter is used if you plan to use the contents of a file as input instead of specifying the test value outright. The contents of the file will be evaluated line by line.

Each parameter makes the switch statement behave differently. Some parameters can be used together, and some override others. The wildcard and regex parameters conflict with each other. When both parameters are used, only the last parameter is used.

### Full Switch Statement Syntax

Building upon the previous section discussing the switch statement parameters, the lines of code below would probably look familiar to you by now.

```powershell
switch [-regex|-wildcard|-exact][-casesensitive] (<value>)
{
    "string"|number|variable|{ expression } { statementlist }
    default { statementlist }
}
```

```powershell
switch [-regex|-wildcard|-exact][-casesensitive] -file <filename>
{
    "string"|number|variable|{ expression } { statementlist }
    default { statementlist }
}
```

You’ll notice from the syntax block above, the `string number variable expression statementlist`. That particularly arranged set of words means that the Powershell switch statement accepts `string`, `number`, `variable` and `expression` as test values. Then the `statementlist` is where the action(s) to execute when the condition matched is listed.

### Using the `-File` Parameter

In this example, the switch statement uses the `file` parameter to use a file as the input for the test value.

Below is an example of the text file named _random.txt_ containing a list of twenty random sentences.

```powershell
She can live her life; however, she wants as long as she listens to what I have to say.
Carol drank the blood as if she were a vampire.
Had he known what was going to happen, he would have never stepped into the shower.
The father died during childbirth.
My dentist tells me that chewing bricks is very bad for your teeth.
When motorists sped in and out of traffic, all she could think of was those in need of a transplant.
He would only survive if he kept the fire going and he could hear thunder in the distance.
The view from the lighthouse excited even the most seasoned traveller.
It was the best sandcastle he had ever seen.
As the years pass by, we all know owners look more and more like their dogs.
Buried deep in the snow, he hoped his batteries were fresh in his avalanche beacon.
You can't compare apples and oranges, but what about bananas and plantains?
She saw no irony asking me to change but wanting me to accept her for who she is.
I don’t respect anybody who can’t tell the difference between Pepsi and Coke.
Dan took the deep dive down the rabbit hole.
On a scale from one to ten, what's your favourite flavour of random grammar?
He hated that he loved what she hated about hate.
I covered my friend in baby oil.
He figured a few sticks of dynamite were easier than a fishing pole to catch fish.
The Japanese yen for commerce is still well-known.
```

Next, the code below will test each line of the _random.txt_ file and display only the sentences that contained the word “she”.

```powershell
switch -file .\random.txt {
    {$_ -match 'she'} { $_ }
}
```

When the code above is run, it will show the below output. As you can see, out of the twenty sentences in the _random.txt_ file, only the sentences with the word “she” is returned.

![Switch statement using the file parameter](/wp-content/uploads/2020/03/switch07-2.gif)

Switch statement using the `file` parameter

### Using the `-Wildcard` Parameter

Using the same file in the previous example named _random.txt,_ this next example will use the `-wildcard` parameter to evaluate the sentences inside the _random.txt_ file contained the word `She`. And to make sure that only the word with uppercase S is returned, the `-casesensitive` parameter is also used.

```powershell
switch -casesensitive -wildcard -file .\random.txt {
    "*She*" { $_ }
}
```

After running the code above, the expected output is shown below. As you can see, only the sentences with matching the wildcard search for `She` (with an uppercase S) is returned.

![Switch statement using the WildCard and CaseSensitive parameters](/wp-content/uploads/2020/03/switch10.gif)

Switch statement using the `WildCard` and `CaseSensitive` parameters

### Using the `-CaseSensitive` Parameter

This example uses the `CaseSensitive` parameter to perform a case-sensitive search of the test value `Banana`. Looking closely into the code below, there are two conditions for the word _banana,_ but one of them start with an uppercase _B_.

```powershell
switch -casesensitive ("Banana") {
    "apple" { "I have $_"; break }
    "orange" { "I have $_"; break }
    "banana" { "I have $_"; break }
    "mango" { "I have $_"; break }
    "Banana" { "I have $_"; break }
    default { "I don't have that fruit" }
}
```

Running the code above, the expected output is shown below.

![Switch statement using the casesenstive parameter](/wp-content/uploads/2020/03/switch09.gif)

Switch statement using the `casesenstive` parameter

### Using the `-RegEx` Parameter

This example demonstrates how to use the `-RegEx` parameter to perform [_regular expression match to determine if the test value is an email address_](https://emailregex.com/).

The text below is the regular expression that will be used to evaluate the test value. This regular expression is saved in a file called _RegExp.txt_.

```powershell
(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-][a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])
```

Below is the list of supposedly email address to validate. This list is saved in a file called _email.txt._

```powershell
june@gmail.com
june@yahoo.com
www.google.com
june@outlook.com
smtp.office365.com
```

Referencing the code below, the first line will import the contents of the _RegExp.txt_ and store it in the `$RegExp` variable. Then, the Powershell switch statement uses the _email.txt_ file as the input for test values as indicated by the `-file` parameter.

```powershell
$RegExp = Get-Content .\RegExp.txt
switch -regex -file .\email.txt {
    $RegExp {"[$_] is an email address"}
    Default {"[$_] is NOT an email address"}
}
```

Once the code above is run in PowerShell, only the test value that matches the regular expression stored in the `$RegExp` will be validated. If the test value did not match the regular expression, the `Default` clause would be triggered. See the sample output below.

![Using Switch RegEx to validate email addresses](/wp-content/uploads/2020/03/switch08.gif)

Using Switch RegEx to validate email addresses

## Conclusion

In this article, you’ve learned the basics of the `switch` statement and how it can be used to evaluate conditions before running any corresponding code. The `switch` statement is also capable of performing more complex tests like evaluating regular expressions and wildcards.

The parameters can also be used together to combine their functionalities for a more controlled result. I hope that in this article, you have gained a new item in your PowerShell toolbox for when you need to write scripts that involve conditional logic and decision making.

Perhaps your next project would be to create a script that reports unhealthy systems or monitoring event logs while using the switch statement. There are a lot of possible uses for the PowerShell switch statement and keep practicing.

Happy scripting!

## Further Reading

*   **_[About\_Switch](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.2)_**
*   **_[About\_Automatic\_Variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2)_**
*   **_[Back to Basics: Conditional Logic with PowerShell If-Else](https://adamtheautomator.com/powershell-if-statement/)_**
*   **_[Email Address Regular Expression That 99.99% Works](https://emailregex.com/)_**

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-switch%2F&text=PowerShell%20Switch%20Statement%3A%20A%20Comprehensive%20Guide)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-switch%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-switch%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
