PSMonday #34: December 19, 2016

Topic: Switch Statement IV

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.

Here we are again. It’s Monday morning and we’re discussing the Switch language construct; we’re wrapping it up, in fact. Last week we determined that Switch included a Wildcard parameter. Today we’ll discuss the Regex parameter, and a bit more.

The below example does a few things. To make this as easiest as possible to follow, I’ve numbered both the list, and the matching code:

  1. Write a string to the host (the screen), to help indicate what the user should do.
  2. Create a prompt to accept the user’s input, and assign that user’s input, to the LastName variable.
  3. Help the user determine their Human Resource representative using a Switch construct and the Regex parameter.
# 1
Write-Output -InputObject 'Enter your last name to determine your Human Resources representative.'

# 2
$LastName = Read-Host -Prompt 'Enter your last name'

# 3
Switch -Regex ($LastName.ToLower()) {
    '^[a-g]' {"Please see Barbara in Room 202."}
    '^[h-m]' {"Please see Mark in Room 203."}
    '^[n-z]' {"Please see Allison in Room 204."}
    default {"$LastName did not work, please try again."}
}

In #3, we’ve set up the Switch statement the same as the other examples; however, we’ve included the Regex parameter. Additionally, the test-value uses the ToLower method, against the value stored in the LastName variable. This ensures our value is in lowercase before we start checking the conditions inside the Switch.

What these conditions do is check the first letter of the last name. Depending on the letter, it’ll report the proper HR representative to visit. One of the things that this example also includes is a default condition. While I could’ve mentioned this week’s ago, I’ve saved it. This is the action to take if there’s no conditional match. Think of this as the Else in an If-Else statement. This could’ve have been included in any of the previous Switch statements we’ve seen in the last three weeks.

Now for the last two examples before we close out the Switch. The $_ variable is something we usually only see when we use ForEach-Object — an upcoming language construct. Even so, it has a purpose in the Switch construct, too. In the below example, it’s acting as the current element in the array. It’s ‘one,’ the first time we enter the Switch, it’s ‘two,’ the second time, etc. Take a look at the example and make sure you can determine why the results were returned the way they were. $PSItem was introduced in PowerShell 3.0, and it does the exact same thing as $_.

$Array = 'one','two','three','four','five'

Switch ($Array) {
    {$_ -eq 'one'} {'One found.'}
    {$PSItem  -eq 'three'} {'Three found.'}
    {$_ -eq 'six'} {'Six found.'}
}

One found.
Three found.

This final example is here to help indicate that we can use the $_, or $PSItem, in both the condition, as we saw above, and in the action, as we’ll see below.

$Servers = 'DC01','DC02','DC03','WEB01'

Switch ($Servers) {
    'DC01'{"Run command against server $_."}
    'DC02' {"Run command against server $_."}
    'WEB01' {"Run command against server $_."}
    {$_} {"Add computer to log file ($_)."}
}

Run command against server DC01.
Add computer to log file (DC01).
Run command against server DC02.
Add computer to log file (DC02).
Add computer to log file (DC03).
Run command against server WEB01.
Add computer to log file (WEB01).

In closing, I should remind everyone that I can’t cover everything about a topic, and so it’s important to finalize your learning, or reviewing, by reading the full about file. You can read about the Switch statement here: Get-Help -Name about_Switch -ShowWindow. Most of it will be a review.

Leave a Reply

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