Hey, Scripting Guy! Question

Hey, Scripting Guy! I want to evaluate a condition and select from several different options. I know I could use the If…Then…ElseIf to do this, but it is somewhat cumbersome and yesterday you said there was a better way to do these kinds of things. What’s the better way?

- KM

SpacerHey, Scripting Guy! Answer

Hi KM,

You are not going to believe this. But the moment I read your question, the Little River Band started playing on my Zune. I love these guys. They are from Melbourne, Australia, and took their name from a town in Victoria. Melbourne, by the way, is a lovely town. The city has trams that make it easy to move around downtown and It has an excellent zoo. Take a look at the following image, which I took while I was down under teaching a scripting class a few years ago.

Image of a bridge in Melbourne, Australia

 

Anyway, the Little River Band is perhaps best known in the United States for one particular song, "Help is on its way." That’s the same song that started playing on my Zune when I read your e-mail message. The scripting cavalry is here!

This week we are looking at scripting Windows PowerShell. Windows PowerShell is installed by default on Windows Server 2008 R2 and Windows 7. It is an optional installation on Windows Server 2008, and a download for Windows Vista, Windows XP, and Windows Server 2003. The Windows PowerShell Scripting Hub is a good place to start using Windows PowerShell. An excellent book for learning Windows PowerShell is the Microsoft Press book, Microsoft Windows PowerShell Step by Step. This book has many hands-on labs and uses real-world examples to show the use of Windows PowerShell.

In VBScript, you would use the Select Case statement to evaluate a condition and select one outcome from a group of potential statements. In the DemoSelectCase.vbs script, the value of the variable a is assigned the value of 2. The Select Case statement is used to evaluate the value of the variable a. The syntax is seen here:

Select Case testexpression

The test expression that is evaluated is the variable a. Each of the different cases contains potential values for the test expression. If the value of the variable a is equal to 1, the code Wscript.Echo "a = 1" is executed. This is seen here:

Case 1
   WScript.Echo "a = 1"

Each of the different cases is evaluated in the same manner. The Case Else expression is run if none of the previous expressions evaluate to true. The complete DemoSelectCase.vbs script is seen here:

DemoSelectCase.vbs

a = 2
Select Case a
 Case 1
   WScript.Echo "a = 1"
 Case 2
   WScript.Echo "a = 2"
 Case 3
   WScript.Echo "a = 3"
 Case Else
   WScript.Echo "unable to determine value of a"
End Select
WScript.Echo "statement after select case"

In Windows PowerShell there is no Select Case statement. There is, however, something that is even better: the Switch statement. The Switch statement is the most powerful statement in the Windows PowerShell language. It is the Swiss Army knife of Windows PowerShell. Let's first examine the basic Switch statement. The Switch statement begins with the Switch keyword, and the condition to be evaluated is positioned inside a pair of smooth parentheses. This is seen here:

Switch ($a)

Next, a pair of braces (curly brackets) is used to mark off the script block for the Switch statement. Inside the script block, each condition to be evaluated begins with a potential value followed by the script block to be executed in the event the value matches the condition. This is shown here:

1 { '$a = 1' }
2 { '$a = 2' }
3 { '$a = 3' }

If no match is found, and the Default statement is not used, the Switch statement exits and the line of code that follows the Switch statement is executed. The Default statement performs a function similar to the one performed by the Case Else statement from the Select Case statement. The Default statement is seen here:

Default { 'unable to determine value of $a' }

The complete DemoSwitchCase.ps1 script is seen here:

DemoSwitchCase.ps1

$a = 2
Switch ($a)
{
 1 { '$a = 1' }
 2 { '$a = 2' }
 3 { '$a = 3' }
 Default { 'unable to determine value of $a' }
}
"Statement after switch"

With the Select Case statement, the first matching case is the one that is executed. As soon as that code executes, the line following the Select Case statement is executed. If the condition matches multiple cases in the Select Case statement, only the first match in the list is executed. In other words, matches from lower in the list are not executed. Therefore, make sure that the most desirable code to execute is positioned highest in the Select Case order.

With the Switch statement in Windows PowerShell, order is not a major design concern. This is because every match from inside the Switch statement will be executed. An example of this is seen in the DemoSwitchMultiMatch.ps1 script:

DemoSwitchMultiMatch.ps1

$a = 2
Switch ($a)
{
 1 { '$a = 1' }
 2 { '$a = 2' }
 2 { 'Second match of the $a variable' }
 3 { '$a = 3' }
 Default { 'unable to determine value of $a' }
}
"Statement after switch"

When the DemoSwitchMultiMatch.ps1 script runs, the second condition and the third condition will both be matched. Therefore, their associated script blocks are executed. The DemoSwitchMultiMatch.ps1 script produces the output seen here:

$a = 2
Second match of the $a variable
Statement after switch

If an array is stored in the variable a in the DemoSelectCase.vbs script, a type mismatch error will be produced. This error is seen here:

Microsoft VBScript runtime error: Type mismatch

The Windows PowerShell Switch statement can handle an array in the variable $a without any modification. The array is seen here:

$a = 2,3,5,1,77

The complete DemoSwitchArray.ps1 script is seen here:

DemoSwitchArray.ps1

$a = 2,3,5,1,77
Switch ($a)
{
 1 { '$a = 1' }
 2 { '$a = 2' }
 3 { '$a = 3' }
 Default { 'unable to determine value of $a' }
}
"Statement after switch"

When the DemoSwitchArray.ps1 script is run, the results seen in this image are produced:

Image of multiple matches produced by a Switch statement

 

If you do not want the multimatch behavior of the Switch statement, you can use the break statement to change the behavior. In the DemoSwitchArrayBreak.ps1 script, the Switch statement will be exited when the first match occurs because each of the match condition script blocks contains the break statement. This is seen here:

 1 { '$a = 1' ; break }
 2 { '$a = 2' ; break }
 3 { '$a = 3' ; break }

You are not required to include the break statement with each condition. Instead, you could use it to exit the Switch only after a particular condition is matched. The complete DemoSwitchArrayBreak.ps1 script is seen here:

DemoSwitchArrayBreak.ps1

$a = 2,3,5,1,77
Switch ($a)
{
 1 { '$a = 1' ; break }
 2 { '$a = 2' ; break }
 3 { '$a = 3' ; break }
 Default { 'unable to determine value of $a' }
}
"Statement after switch"

When the DemoSwitchArrayBreak.ps1 script runs, the output in this image is seen:

Image of the Switch statement being exited upon first match

 

KM, that is the Windows PowerShell Switch statement. Today we looked at some of the mundane uses for the Switch statement. A more interesting use of the Switch statement can be seen in this “Hey, Scripting Guy!” article: How Can I Identify All Local Users, Groups, and Services on a Local Computer? In that article, we use regular expressions to do a pattern match; we use the Switch statement to maintain a running count of local directory objects. We hope that you have enjoyed this excursion into Windows PowerShell decision-making. Join us tomorrow as we dive into the mailbag and emerge with a handful of queries worthy of Quick-Hits Friday.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys