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:

[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:

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:

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:

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:

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.

– 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.

– 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.

– 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.

– 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:

[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:

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:

[adinserter name=”In Article”]

File

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:

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:

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

[adinserter name=”Matched-Content”]