PSMonday #31: November 28, 2016

Topic: Switch Statement

Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.

The Switch language construct, also called the case statement in other languages, is a close relative to the If statement and its variations. As you work with If, be sure to consider Switch, as it can assist in a cleaner layout, and therefore, more easily read code. As mentioned previously, if your If statements are going past three levels, consider the Switch.

To start, here’s the basic, conceptual format. We consider a test-value in the parenthesis, determine the matching condition on the left, and then execute its corresponding action on the right.

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

To begin, here’s a very basic example. We evaluate the test-value — it’s just a numeric 3 — and then find the matching condition. Once found (the 3 in our list of conditions), we’ll take the corresponding action, and write the word “Three” to the screen. Simple.

Switch (3) {
    1 {'One'}
    2 {'Two'}
    3 {'Three'}
    4 {'Four'}
}

Three

Our test-value can also be an equation: 2 + 2 (= 4) and then down through the possible conditions, and take the appropriate action.

Switch (2 + 2) {
    1 {'One'}
    2 {'Two'}
    3 {'Three'}
    4 {'Four'}
}

Four

There’s something we need to understand about the Switch construct, and that is that it is designed to look at all the conditions, no matter if it’s found one already, or not. Take a look at this example for clarification.

Switch (3) {
    1 {'One'}
    2 {'Two'}
    3 {'Three'}
    4 {'Four'}
    3 {'Three (again).'}
    5 {'Five'}
}

Three
Three (again).

In the above example, we can easily see that although it found 3 in the list of conditions, it continued looking through the additional conditions, only to find another one that matched. What we should have done, is just included multiple commands inside the action, like the following two examples. Notice that in the first example, we use the semi-colon as a command separator, and in the second example, we use the line break (Enter).

Switch (3) {
    1 {'One'}
    2 {'Two'}
    3 {'Three'; 'Three (again, but not really).'}
    4 {'Four'}
}

Three
Three (again, but not really).

Switch (3) {
    1 {'One'}
    2 {'Two'}
    3 {'Three'
       'Three (again, but not really).'
    }
    4 {'Four'}
}

Three
Three (again, but not really).

Make sure you fully understand these examples before next week. We’ll start that PSMonday in an effort to show how to get our Switch statements to act a bit more like the If statement, to exit when our condition is matched, and action taken.

Leave a Reply

Your email address will not be published.