Summary: Microsoft Scripting guy, Ed Wilson, talks about using the Windows PowerShell Switch statement to clean up script in a wrap up of the 2014 Winter Scripting Games.
Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I have been busy planning our trip to Houston for the 2014 North American TechEd Conference. The Scripting Guys will have a booth on the conference floor, and the Scripting Wife and I will be there along with Marc Adam Carter and Jaap Brasser. This is a really cool team, and you will want to stop by and chat about Windows PowerShell. The Scripting Wife will be putting out our booth schedule as we get closer to the event. For now, stay tuned.
Note This is the fifth in a series of blog posts in which I talk about things I noticed whilst grading submissions for the
2014 Winter Scripting Games. In case you missed the previous episodes:
- 2014 Winter PowerShell Scripting Games Wrap Up #1
Talked about best practices for using aliases and for formatting of Windows PowerShell scripts. - 2014 Winter PowerShell Scripting Games Wrap Up #2
Talked about the need for and the use of structured error handling. - 2014 Winter PowerShell Scripting Games Wrap Up #3
Talked about using standard Windows folders from within Windows PowerShell scripts. - 2014 Winter PowerShell Scripting Games Wrap Up #4
Talked about using the Write-Verbose and Write-Error streams in Windows PowerShell scripts.
If If IF / else Else ELSE
For the record, there is nothing wrong with doing if if if / else else else in a Windows PowerShell script. In fact, it is fast, and it is what Windows PowerShell really does behind the scenes anyway. So what is the big deal about Switch? Well, it simplifies the script, makes it easier to read, and it is less cluttered.
In the following function, I create a sample if/elseif/else structure to evaluate the value passed to the function:
Function Example-If
{
Param ($a)
If ($a -eq 0) { "0 is the number" }
ElseIf ($a -eq 1) {"1 is the number" }
ElseIf ($a -eq 2) {"2 is the number"}
Elseif ($a -eq 3) {"3 is the number"}
Else {"Not sure what the value of `$a really is"}
}
Although there is nothing wrong with the script, it was a decent amount of writing to do, and it is also a bit cumbersome to read. Not only that, it is a bit error prone due to the constant repetition. When I run the script, the output that is shown in the following image appears.
To use the Switch statement, I begin with the word Switch. Next I include the condition that I will evaluate, followed by a script block that defines each potential value and what I will do when the condition comes True. Following is an example of using the Switch statement:
Function Example-Switch
{
Param($a)
Switch ($a)
{
0 {"0 is the number"}
1 {"1 is the number"}
2 {"2 is the number"}
3 {"3 is the number"}
Default {"Not sure what the number is"}
}
}
The image that follows is an example of using the Example-Switch function and the output associated with the function.
If you do not remember the syntax for the Switch statement, it is easy enough to use the built-in snippets from the Windows PowerShell ISE to add-in a framework of script. This is shown in the image that follows.
All I need to do to get to the script snippets in the Windows PowerShell ISE is to click Edit, and then click Start Snippets, or I can press Ctrl plus J to bring up the snippet dialog box. I can then use the Down arrow to move through the collected snippets until I arrive at the Switch snippet. If I hover long enough, then IntelliSense will pop up a box that shows what the script snippet looks like. For more details, see the Hey, Scripting Guy! Blog post by Windows PowerShell MVP, Jonathan Medd, Using PowerShell ISE Snippets to Remember Tricky Syntax.
That is all there is to using the Windows PowerShell Switch statement to clean up your script. The 2014 Winter Scripting Games Wrap-Up Week continues tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy