• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Switch

PowerShell Switch

08/10/2018 by Stephanos 2 Comments

PowerShell Switch

In this tutorial we will see about PowerShell Switch statement and how we are able to use it.

When we need to check a condition we are using If statement. In cases we would like to check multiple conditions we can use a Switch statement. The switch statement perform the same job as a series of if statements but it is simpler to use it. In the switch statement you are listing each condition to be checked and optionally a default action, in case none of the conditions is met. Below we will see the syntax of the PowerShell Switch statement and how we are able to use it.

Basic Syntax

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

Example 1

Code:

$TestValue = 5
switch ($TestValue){
    1 {"One"}
    2 {"Two"}
    3 {"Three"}
    4 {"Four"}
    5 {"Five"}
    6 {"Six"}
    7 {"Seven"}
    8 {"Eight"}
    9 {"Nine"}
    10 {"Ten"}
}

Output:

PowerShell Switch - Example 1

[adinserter name=”In Article”]

Example 2

Switch statement, by default, checks the value against all condition. If a condition is matched multiple times, it will take action for each of the conditions.

Code:

$TestValue = 5
switch ($TestValue){
    1 {"One"}
    2 {"Two"}
    3 {"Three"}
    4 {"Four"}
    5 {"Five"}
    6 {"Six"}
    7 {"Seven"}
    8 {"Eight"}
    9 {"Nine"}
    5 {"Five-Five"}
    10 {"Ten"}
}

Output:

PowerShell Switch - Example 2

Example 3

In case we would like to stop the switch statement to further check after the first match we need to use Break.

Code:

$TestValue = 5
switch ($TestValue){
    1 {"One"}
    2 {"Two"}
    3 {"Three"}
    4 {"Four"}
    5 {"Five";Break}
    6 {"Six"}
    7 {"Seven"}
    8 {"Eight"}
    9 {"Nine"}
    5 {"Five-Five"}
    10 {"Ten"}
}

Output:

PowerShell Switch - Example 3

We are able to provide multiple value to be checked by the switch statement. The order that we will provide the values is the order that switch statement will check the values. An array of values can also be provided to the switch statement and it will go through the switch for each of the values. Note that if you use any Break statement within the action, switch will stop checking the rest of the values. Check the below two examples.

[adinserter name=”In Article”]

Example 4

Code:

$TestValue = 5
$TestValue2 = 2
switch ($TestValue,$TestValue2){
    1 {"One"}
    2 {"Two"}
    3 {"Three"}
    4 {"Four"}
    5 {"Five"}
    6 {"Six"}
    7 {"Seven"}
    8 {"Eight"}
    9 {"Nine"}
    10 {"Ten"}
}

Output:

PowerShell Switch - Example 4

Example 5

Code:

$TestValue = 5
$TestValue2 = 2
$TestValue3 = 1
$TestValue4 = 9
$TestValue5 = 8
$Array = @($TestValue,$TestValue2,$TestValue3,$TestValue4,$TestValue5)
switch ($Array){
    1 {"One"}
    2 {"Two"}
    3 {"Three"}
    4 {"Four"}
    5 {"Five"}
    6 {"Six"}
    7 {"Seven"}
    8 {"Eight"}
    9 {"Nine"}
    10 {"Ten"}
}

Output:

PowerShell Switch - Example 5

Above, we saw basic examples of switch statement and how we are able to use it. Switch statement has some parameters that we can use to have a specific behaviour of our switch statement. The advance syntax of switch statement is below:

[adinserter name=”In Article”]

Advance Syntax

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

Lets see what each of the parameters does. The description is provided by Microsoft.

Switch Parameters

  • Wildcard – Indicates that the condition is a wildcard string. If you use Wildcard, Regex and Exact are ignored. Also, if the match clause is not a string, the parameter is ignored.
  • Exact – Indicates that the match clause, if it is a string, must match exactly. Regex and Wildcard are ignored. Also, if the match clause is not a string, this parameter is ignored.
  • CaseSensitive – Performs a case-sensitive match. If the match clause is not a string, this parameter is ignored.
  • File – Takes input from a file rather than a value statement. If multiple File parameters are included, only the last one is used. Each line of the file is read and evaluated by the Switch statement.
  • Regex  – Performs regular expression matching of the value to the condition. Wildcard and Exact are ignored. Also, if the match clause is not a string, this parameter is ignored.

Wildcard

Example 6

Code:

$TestValue = "Hello World"
switch -Wildcard ($TestValue){
    "Hello" {"Hello"}
    "My World" {"My World"}
    "*World" {"World Wildcard"}
    "HelloWorld" {"HelloWorld"}
    "Hello*" {"Hello Wildcard"}
}

Output:

PowerShell Switch - Example 6

[adinserter name=”In Article”]

CaseSensitive

Example 7

By default switch statement is case insensitive. By using -CaseSensitive you can have case sensitive matches only.

Code:

$TestValue = "Hello World"
switch -CaseSensitive ($TestValue){
    "hello world" {"hello world"}
    "hello World" {"hello World"}
    "Hello World" {"Hello World"}
    "Hello world" {"Hello world"}
    "HELLO WORLD" {"HELLO WORLD"}
    "HeLlO WoRld" {"HeLlO WoRld"}
}

Output:

PowerShell Switch - Example 7

Regex

Example 8

Code:

$TestValue = "Hello World"
switch -Regex ($TestValue){
    "hello\sworld" {"First"}
    "hello World" {"Second"}
    "Hello W\w\w\wd" {"Third"}
    "HelloWorld" {"Forth"}
    "H\dlloWorld" {"Fifth"}
}

Output:

PowerShell Switch - Example 8

[adinserter name=”In Article”]

File

PowerShell Switch - Example 9-1

Code:

$Path = "C:\Scripts\testfile.txt"
switch -File $Path {
    "hello\sworld" {"First"}
    "hello World" {"Second"}
    "Hello W\w\w\wd" {"Third"}
    "HelloWorld" {"Forth"}
    "H\dlloWorld" {"Fifth"}
}

Output:

PowerShell Switch - Example 9-2

Default

The default action is optional. If we specify a default action and there is no match, the switch statement will run the default action.

Code:

$TestValue = "MyValue"
switch ($TestValue) {
    "Hello World" {"First"}
    "Second Condition" {"Second"}
    "Third Condition" {"Third"}
    "Value" {"Forth"}
    "None" {"Fifth"}
    default {"There is no match"}
}

Output:

PowerShell Switch - Example 10

I hope the tutorial about PowerShell Switch is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Switch | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Switch
Article Name
PowerShell Switch
Description
PowerShell Switch. In this tutorial you will find information about PowerShell Switch and its use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: PowerShell Switch

Reader Interactions

Comments

  1. Logan says

    09/10/2018 at 04:47

    Just learning PS so this was great to read!

    Reply
    • Stephanos says

      09/10/2018 at 08:58

      Thanks for your feedback

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact