Powershell

PowerShell switch statement

The switch statement works just like an “if” statement. The difference between the switch and if-statement is that the switch is used to check several conditions in an easier way. In simple words, it basically switches the condition. The switch statement compares every condition of the given list. When it finds the best matches against the test value, it returns as output.

In this article, we will explain the use of PowerShell Switch statements with some practical examples.

How to use Switch Statement in PowerShell?

PowerShell is enriched with a bunch of useful programming functionalities and the switch statement is one of those. The switch statement is a sort of “if” conditional statement, which iterates till it finds its best match in the given conditions.

There are some examples to explain the basic concept of the Switch statement, written as follows.

Example 1: How to print a specific case using a switch statement (with an integer)?

In this example, we will explain the function of switch statements to print the specific case. For instance, the following code is used to print the value placed at “5”.

switch (5)

{

1 {"Monday"}

2 {"Tuesday"}

3 {"Wednesday"}

4 {"Thursday"}

5 {"Friday"}

6 {"Saturday"}

7 {"Sunday"}

}

In the above code, there are a total of seven switch cases. We have used the switch (5) which will only consider the case at the 5th position.

The switch statement iterates over all the cases. When it encountered case#5, it instantly printed the “Friday” as it was stored at case#5.

Example 2: How to print a specific case using switch statement (with strings)?

As you know, the string values are enclosed with double quotes (“ ”). In this example, we will see how the switch function is working with the string values. The following code makes use of a string object to match the cases using the switch statement.

switch ("day4")

{

day1 {"Monday"}

day2 {"Tuesday"}

day3 {"Wednesday"}

day4 {"Thursday"}

day5 {"Friday"}

day6 {"Saturday"}

day7 {"Sunday"}

}

In the above code, the day1 to day7 refers to the weekdays. The switch keyword searches and matches the “day4” in various cases.

As per the above output, it can be observed that the test value (day4) matched with the 4th condition which is Thursday.

Example 3: How to match only one occurrence of a specific case?

If there are multiple occurrences of a case, the switch statement prints all the possible matches. However, you can use the “Break” keyword to print and terminate the further matching. The example code of switch with Break statement is provided below:

switch ("day5")

{

day1 {"Monday"}

day2 {"Tuesday"}

day3 {"Wednesday"}

day4 {"Thursday"}

day5 {"Friday"; Break}

day6 {"Saturday"}

day7 {"Sunday"}

day5 {"Friday"}

day5 {"Friday"}

day5 {"Friday"}

}

In the above code, there are multiple cases that match with the “day5”. We have used the Break statement with the first occurrence of the “day5”.

After running the script, the test value is compared to the match value in the list. And terminated the function when it found the “Break” statement. It can be noticed from the above output, that the switch statement returned the first Match in their output and terminated the function.

Example 4: How to match multiple cases using the switch statement?

The switch statement also works if the test value is a collection, which means it has more than one test value. The example script is as follows.

switch ("day2","day4","day6")

{

day1 {"Monday"}

day2 {"Tuesday"}

day3 {"Wednesday"}

day4 {"Thursday"}

day5 {"Friday"}

day6 {"Saturday"}

day7 {"Sunday"}

default {

"Please select within seven days"}

}

As per the above-mentioned output, it can be observed that the switch statement returns the values that match with multiple cases.

Example 5: How to set a default condition?

In the switch statement you can set a condition as a default clause. This action will be performed when the test value does not find its matches in the given conditions.

switch ("day8")

{

day1 {"Monday"; Break}

day2 {"Tuesday"; Break}

day3 {"Wednesday"; Break}

day4 {"Thursday"; Break}

day5 {"Friday"; Break}

day6 {"Saturday"; Break}

day7 {"Sunday"; Break}

day1 {"Monday"}

day2 {"Tuesday"}

day3 {"Wednesday"}

day4 {"Thursday"}

day5 {"Friday"}

day6 {"Saturday"}

day7 {"Sunday"}

default {

"Please select within seven days"}

}

In the above code, there are a total of seven cases (day1 to day7). However, “day8” is not present in the list. Lastly, we have added a “default” clause.

The above-given snippet shows that the switch statement does not find the match(day8) so the default case gets executed.

Example 6: How to use Switch with Wildcard?

The -Wildcard parameter enables the switch instructions to match wildcards to available conditions. The Wildcard compares the conditions with an asterisk “ * ” sign. The example is as follows.

$country = Read-Host "Enter your favorite country"

switch -wildcard ($country)

{

Pa* {"Pakistan"}

Pa* {"Palau"}

Pa* {"Panama"}

Chi* {"China"}

Fra* {"France"}

}

In the above code, the value is taken as an input from the user. The switch statement considers the input with the “-wildcard” parameter to match the available cases.

The above-attached output shows that the user has input the keyword “pa” and the “-wildcard” parameter matches the pattern with the existing cases and has printed the cases that start with the “pa” keyword.

Here you go! You have learned the way to use a PowerShell switch statement.

Conclusion

The PowerShell Switch statement provides an easier way to find the best match against the test expressions. Switch statement works on the base of logical statements like if/else. This article presented detailed learning material about the PowerShell Switch statement. Switch cases can be used to match single or multiple cases. Additionally, it can also be used to get only specific cases that match a pattern (using the wildcard).

About the author

Adnan Shabbir