PowerShell switch command: Turn it on for endless possibilities

PowerShell is all about making repetitive tasks more efficient. But there are times when you can use PowerShell commands to make PowerShell more efficient. Many times you’d want your PowerShell script to execute a particular line or block of code based on a certain condition. When you think of conditions, the first statement that comes to your mind is the ElseIf statement. While this is good to handle one or two conditions, it can get cumbersome when you have to handle more than about three conditions. Instead of writing many ElseIf statements, you can use another powerful command: PowerShell switch.

In fact, PowerShell switch is a powerful statement that controls the flow of execution through your script. Also, the PowerShell switch command comes with many features and options not found in other programming languages.

So, let’s get down to business and see how we can use this “switch” command for a clean, understandable, and powerful code.

Why use PowerShell switch?

PowerShell switch

Before we get into coding, why should you use the switch command in the first place? Let’s see an example. Let’s say you want to print a message based on the input value.

$student_number = 4
if ( $student_number -eq 0 ) { $name = ‘John’ }
elseif ( $student_number -eq 2 ) { $name = ‘Sam’    }
elseif ( $student_number -eq 3 ) { $name = ‘Jess’   }
elseif ( $student_number -eq 3 ) { $name = ‘Tim’ }
elseif ( $student_number -eq 4 ) { $name = ‘Laura’  }
$name

Here, the output would be Laura, but the above code is cumbersome and doesn’t scale well. Imagine you have 100 children in a school. Even the very thought of writing 99 ElseIf statements makes it a dizzy experience!

The switch statement is a cleaner and easier way to get the same output.

$student_number = 4
$name =   switch ( $student_number )
{
0 { ‘John’    }
1 { ‘Sam’    }
2 { ‘Jess’   }
3 { ‘Tim’ }
4 { ‘Laura’  }
}
$name

Even a quick glance shows how easy and clean this code is when compared to the previous one.

Besides providing easier code, many other advantages come with the PowerShell switch command.

Adding the default value

Many times, you’d want a default value that will be displayed if the input doesn’t match any of the existing values. This default value prevents the possibility of errors and can also tell you if there’s a problem with the input.

You can add the default value in the above code like this:

$name =   switch ( $student_number )
{
0 { ‘John’    }
1 { ‘Sam’    }
2 { ‘Jess’   }
3 { ‘Tim’ }
4 { ‘Laura’  }
default  { ‘no match between the name and input value’  }
}
$name

Though this option is available in other conditional statements too, it is easy to code it when you use the switch statement.

Using any input

Another advantage is that you can use any input and not just numbers. For example, you can use strings as your input value.

$profession = ‘writer’
$salary =   switch ( $profession )
{
Writer { ‘$50,000’    }
Programmer { ‘$60,000’    }
Data-entry { ‘$40,000’   }
}
$salary

To make life easy for you, PowerShell switch accepts arrays as inputs. All that you have to do is give an array as the input and the switch statement will run through each value of the array and will execute that block of code that matches the input.

$profession = @(‘writer’, ‘programmer’)
$salary =   switch ( $profession )
{
Writer { ‘$50,000’    }
Programmer { ‘$60,000’    }
Data-entry { ‘$40,000’   }
$salary
}

The output will be:

$50,000

$60,000

Even if you have multiple entries in your array for the same value, it will be matched and that code block will be executed.

$profession = @(‘writer’, ‘programmer’)
$salary =   switch ( $profession )
{
Writer { ‘$50,000’    }
Programmer { ‘$60,000’    }
writeR { ‘$40,000’   }
$salary
}

The output will be:

$50,000

$60,000

$40,000

If you’re using PowerShell 5.0, you can also enums and hashtables, besides arrays.

Multiple parameters

The biggest advantage with Powershell switch is the many parameters it can support and the flexibility it can give you as a programmer. These parameters give you complete control over how your code should flow.

Some of the popular parameters of PowerShell switches are:

-CaseSensitive: By default, the matches are not case sensitive. But you can change this behavior with the -CaseSensitive parameter

-Wildcard: This parameter indicates that the condition is a wildcard string and this is ignored if the matched clause is not a string. This parameter is not case sensitive.

-Exact: This parameter ensures that the input is matched exactly with the values and this is also not case-sensitive.

-File: Takes the input value from a file instead of from the input stream. The comparison is not case-sensitive and if you specify multiple files, only the last one is used as input.

-Regex: Matches a regular expression to the value of a condition

In the -regex and -wildcard parameters, only the last parameter is taken if you specify multiple parameters.

As you can see, these aspects make the PowerShell switch condition a versatile and powerful tool to meet your programming needs.

Advanced uses of PowerShell switch

So far, we have seen some of the different ways to use a PowerShell switch and most of it covered above are fairly basic. In reality, the PowerShell switch gives the option for many advanced actions as well, and some of them are explained below.

Multiple match scenarios

You may have already guessed by now, but if you haven’t, PowerShell switch is a great option for multiple matches. For example, let’s say,

$profession = ‘writer’
$salary =   switch ( $profession )
{
Writer { ‘$50,000’    }
Programmer { ‘$60,000’    }
writer { ‘$40,000’   }
}
$salary

The output will be:

$50,000

$40,000

The above output goes to show that every condition is checked in the same programming order and each condition is evaluated against the input value. This is applicable for array inputs too, where every value of the array is checked against every condition in your code.

Using expressions

The input you give for a switch statement doesn’t have to be a single value or an array. It can even be a complex expression and the final value of that expression is considered for the match. Imagine the flexibility this feature can give you while programming real-life applications where the output may change dynamically and you want your code to react accordingly.

Break and continue

Both these are powerful commands that are sure to come in handy while processing large values and arrays. At the most fundamental level, the break command will exit out of the switch while the continue command will move to the next item in the array.

$profession = @(‘writer’, ‘programmer’)
$salary =   switch ( $profession )
{
Writer { ‘$50,000’    }
continue
Programmer { ‘$60,000’    }
writer { ‘$40,000’   }
$salary
}

In this case, the output will be:

$50,000

$60,000

Why? Because after the match is made the program moves to the next item in the array and doesn’t process the rest of the code.

$profession = @(‘writer’, ‘programmer’)
$salary =   switch ( $profession )
{
Writer { ‘$50,000’    }
break
Programmer { ‘$60,000’    }
Data-entry { ‘$40,000’   }
$salary
}

In this case, the output is:

$50,000

Why? Because the code exits the switch loop after it encounters the word “break.”

Both these statements give you a ton of flexibility as a programmer.

PowerShell switch: Make it your own

PowerShell switch is a powerful command that offers a ton of benefits to every programmer. We hope this article enthuses you to use more of this command in your programming.

About The Author

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top