Check multiple conditions, an alternative to using multiple if statements.
Brief Syntax Switch ($item) { value { expression } value { expression } } Full Syntax Switch [ -regex | -wildcard | -exact ] [-casesensitive] ( pipeline ) { "string" | number | variable | { expression } { statementlist } [ default { statementlist } ] } Switch [ -regex | -wildcard | -exact ] [-casesensitive] -file filename { "string" | number | variable | { expression } { statementlist } [ default { statementlist } ] } Key -regex Treat the match clause, if a string, as a Regex. default This provides an optional 'catch-all' for any data items that are not explicitly handled in the list of switch statements. Append 'default {statementlist}' as the last choice. The 'default' will only be used if no match is found for the other conditions. -wildcard Treat the match clause, if a string, as a wildcard string Wildcard characters: ? = Any single unspecified character. * = Zero or more unspecified chars. [a-b] = Any character in the range a-b [ab] = The specified characters. The wildcard match is case in-sensitive -exact Match strings exactly (disable wildcards) -casesensitive Modify the match clause, if a string, to be case sensitive -file Take input from a file (or representative)
The switch options can be given in abbreviated format using the first letter, so -e is equivalent to -exact
The keyword break indicates that no more processing will occur and the switch statement will exit.
The keyword continue indicates that no processing will continue against the current token and the next token in the conditional will be evaluated. If no tokens are available, the switch statement will exit.
If pipeline results in an array, each element of the array will be evaluated in ascending offset order (starting at 0). At least one element must be present that meets at least one condition or an error will result.
Examples
To compare two values, a string, a number or anything else we can use a simple IF statement:
$fruit = "orange"
If ($fruit -eq "orange") {'We found an orange'}
For this example we are just echoing a line of text, but the expression could be anything, copying a file, starting a service whatever you can think of.
To test more than one item, we can extend this with one or more Elseif() statements:
If ($fruit -eq "orange") {'We found an orange'}
ElseIf ($fruit -eq "apple") {'We found an apple'}
ElseIf ($fruit -eq "pear") {'We found a pear'}
So far so good, but for more than a couple of tests IF and ElseIf become quite verbose, we had to repeat the variable name $fruit on every line. The switch statement does exactly the same thing with much less typing:
$fruit = "orange" switch ($fruit) { "apple" {"We found an apple"; break} "pear" {"We found a pear"; break} "orange" {"We found an orange"; break} "peach" {"We found a peach"; break} "banana" {"We found a banana"; break} default {"Something else happened"; break} }
To store the output of switch in a variable, this can be assigned right at the start using the pipeline, rather than in every script block.
$fruit = "orange" $result = switch ($fruit) { "apple" {"We found an apple"; break} "pear" {"We found a pear"; break} "orange" {"We found an orange"; break} "peach" {"We found a peach"; break} "banana" {"We found a banana"; break} default {"Something else happened"; break} } $result
The break at the end of each condition tells switch to stop looking further, if you omit this, the switch statement will continue testing all the other options and can potentially match more than one clause and perform more than one action.
When writing a switch clause without a break, it can be useful to add a # FALLTHROUGH comment to make it clear that subsequent tests will still be performed.
$drink = 'Gin and Tonic' $result = switch -wildcard ($drink) { "Gin*" {"Contains Gin"} # FALLTHROUGH "Whisky*" {"Contains Whisky"} # FALLTHROUGH "*tonic" {"Contains Tonic"} # FALLTHROUGH }
$result
Passing an array to a switch statement will process each element in the collection. So to repeat the last example just change the first line to an array of different drinks:
$drink = 'Gin and Tonic', 'Vodka and Tonic'
The $result returned will then be an array of all the results - if you need to identify which result is which, consider including $_ or $psitem in the value you return. e.g. "$_ Contains Gin"
The switch statement can process a file with the -File parameter. This works the same as passing an array of values, each line in the text file will be passed to switch.
In this example we read a text file that contains nothing but a list of error numbers, and translate them into a text message:
$errlog = 'C:\logs\errorlog.txt' switch -File $errlog { 10 { "Error ten found"; break } 50 { "Error fifty found"; break } 100 { "Error one hundred"; break} }
Notice that because these switch clauses use break, as soon as any one number is matched, no further processing is done. The break does not just complete the current item and skip to the next, it breaks all processing and exits the switch statement.
"History teaches us that men and nations behave wisely once they have exhausted all other alternatives" ~ Abba Eban
Related PowerShell Cmdlets:
Break statement
Continue statement
ForEach-Object - (foreach) Loop for each object in the pipeline.
ForEach - Loop through values in the pipeline.
For - Loop through items that match a condition.
IF - Conditionally perform a command.
While - Loop while a condition is True.