PSMonday #33: December 12, 2016

Topic: Switch Statement III

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.

In case you haven’t noticed, I really like the Switch statement. Let’s start today’s PSMonday with this first example. It’s dead simple, and really only here for two reasons. One, it indicates we can use the semi-colon between sets of conditions and actions, and two, because I want to indicate the importance of the break statement. After the Switch finds its match in the third condition, it’s going to continue to check the other 23 conditions.

Switch ('c') {
    'a' {'A'}; 'b' {'B'}; 'c' {'C'}; 'd' {'D'}; 'e' {'E'}
    'f' {'F'}; 'g' {'G'}; 'h' {'H'}; 'i' {'I'}; 'j' {'J'}
    'k' {'K'}; 'l' {'L'}; 'm' {'M'}; 'n' {'N'}; 'o' {'O'}
    'p' {'P'}; 'q' {'Q'}; 'r' {'R'}; 's' {'S'}; 't' {'T'}
    'u' {'U'}; 'v' {'V'}; 'w' {'W'}; 'x' {'X'}; 'y' {'Y'}
    'z' {'Z'}
}

C

Let’s avoid that unnecessary work. In this example, after it hits the correct condition, it’ll leave the Switch statement. While there’s not much time savings in this example, there could be, if the Switch was doing more actions than simply echoing a letter to the screen. That’s something to keep in mind as you’re putting these together and considering the completion time of your code.

Switch ('c') {
    'a' {'A'; break}; 'b' {'B'; break}; 'c' {'C'; break}
    'd' {'D'; break}; 'e' {'E'; break}; 'f' {'F'; break}
    'g' {'G'; break}; 'h' {'H'; break}; 'i' {'I'; break}
    'j' {'J'; break}; 'k' {'K'; break}; 'l' {'L'; break}
    'm' {'M'; break}; 'n' {'N'; break}; 'o' {'O'; break}
    'p' {'P'; break}; 'q' {'Q'; break}; 'r' {'R'; break}
    's' {'S'; break}; 't' {'T'; break}; 'u' {'U'; break}
    'v' {'V'; break}; 'w' {'W'; break}; 'x' {'X'; break}
    'y' {'Y'; break}; 'z' {'Z'}
}

C

Instead of putting the true test-value in the parenthesis, we’ll use a variable in the next example. Additionally, we’ll take a look at the Switch’s wildcard parameter. In this example, we’re looking to match multiple conditions. Read it over, and we’ll run through the logic just below the example.

$Building = 'A-Center'

Switch -Wildcard ($Building) {
    'A*' {'Building: A'}
    'B*' {'Building: B'}
    'C*' {'Building: C'}
    'D*' {'Building: D'}
    '*North' {'Location: North'}
    '*South' {'Location: South'}
    '*Center' {'Location: Center'}
    '*East' {'Location: East'}
}

Building: A
Location: Center

The first four conditions, A*, B*, C*, and D*, are in place to match the beginning of the string that was assigned to the Building variable. The first condition, A*, is a match. The last four conditions in the Switch are in place to match the end of the string. Of those, the ‘*Center’ condition is a match. Like it does in other places, the asterisk is a wildcard character; it stands in, for one or more characters.

Here’s the same example, except that we’re outputting the appropriate actions to a variable, instead of writing them to the host. See if you can follow it. Notice that += assignment operators used in the last four conditions. This appends the value to the already existing value assigned to the Location variable.

$Building = 'A-Center'

Switch -Wildcard ($Building) {
    'A*' {$Location = 'Building: A'}
    'B*' {$Location = 'Building: B'}
    'C*' {$Location = 'Building: C'}
    'D*' {$Location = 'Building: D'}
    '*North' {$Location += ', Location: North'}
    '*South' {$Location += ', Location: South'}
    '*Center' {$Location += ', Location: Center'}
    '*East' {$Location += ', Location: East'}
}

$Location

Building: A, Location: Center

There’s still more to cover. We’ll be back with a fourth installment of the Switch language construct next week.

Leave a Reply

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