Switch


If you find yourself needing to check multiple conditions or otherwise create a lengthy If and ElseIf statement, then you need to use PowerShell's Switch statement. This statement acts like many If statements. If you have experience with VBScript, you'll recognize this construct as a Select Case statement. By default Switch is not case-sensitive. Here's a quick example:

 PS C:\> $var=5 PS C:\> switch ($var) { >> 1 {"Option 1"} >> 2 {"Option 2"} >> 3 {"Option 3"} >> 4 {"Option 4"} >> 5 {"Option 5"} >> } >> Option 5 PS C:\> 

The Switch statement evaluates the contents contained within parentheses, and then the condition or value is matched against a set of expressions contained within braces. Each expression has a corresponding block of code in braces. If the expression matches, then command processing switches to the corresponding code.

In this example, the code that corresponds to the matching expression is executed since the value of $var is 5. If there is no match, then nothing will be displayed. However, you can use default at the end of the Switch statement to execute code in the event no other matches are made:

 PS C:\> $var=5 PS C:\> switch ($var) { >> 1 {"Option 1"} >> 2 {"Option 2"} >> 3 {"Option 3"} >> Default {"No match"} >> } >> No match PS C:\> 

In this variation the default code block is executed since there is no matching expression for the value of $var.

Typically you will write out Switch statements in separate lines to make it easier to read and troubleshoot. However, you could just as easily write a statement like this:

 Switch ($i) {1 {"Option 1"} 2 {"Option 2"} 3{"Option 3"}} 

This statement will evaluate $i by looking for 1, 2, or 3, and then executing the corresponding code.

The Switch statement also supports additional options that are outlined in Table 8.1:

image from book
Table 8.1: Switch Options
Open table as spreadsheet

Option

Description

-regex

Indicates that the match clause, if a string, is treated as a regular expression string. Using this parameter disables -wildcard and -exact. If the variable to be evaluated is not a string, this option is ignored.

-wildcard

Indicates that if the match clause is a string, it is treated as a -wildcard string. Use of this parameter disables -regex and -exact. If the variable to be evaluated is not a string, this option is ignored.

-exact

Indicates that if the match clause is a string, it must match exactly. Use of this parameter disables -wildcard and -regex. If the variable to be evaluated is not a string, this option is ignored.

-casesensitive

If the match clause is a string, modify it to be case sensitive. If the variable to be evaluated is not a string, this option is ignored.

-file

Take input from a file (or representative) rather than statement. If multiple -file parameters are used, the last one is be used. Each line of the file is read and passed through the switch block.

image from book

The complete switch syntax can be one of the following:

 switch [-regex|-wildcard|-exact][-casesensitive] (pipeline) 

or

 switch [-regex|-wildcard|-exact][-casesensitive] -file filename { "string"|number|variable|{ expression } { statementlist } default { statementlist } 

Let's look at a quick example.

SwitchRegex.ps1

image from book
 #SwitchRegex.ps1 $var="PowerShell123","PowerShell","123","PowerShell 123" Switch -regex ($var) { "^\w+[a-zA-Z]$" {write-host $_" is a word"} "^\d+$" {write-host $_" is a number"} "\s" {write-host $_" has a space"} Default {write-host "No match found for"$_} } 
image from book

In this script we set a variable with several different values. The Switch statement uses the regex option, which tells PowerShell we will be matching based on regular expressions. A different message is displayed depending on the match:

 No match found for PowerShell123 PowerShell is a word 123 is a number PowerShell 123 has a space PS C:\> 

If a Switch statement will result in multiple matches, then each match block of code will be executed. If this is not your intention, then you need to use the Break or Continue keywords, which are covered below.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net