Switches
Switches enable you to quickly test multiple scripting scenarios without actually writing if
statements with comparison operators. Switches are the most efficient flow control commands as they can quickly funnel data into different code sections based on an item. The Switch
command allows you to evaluate the contents of a single variable and execute subsequent tasks based on the value of the variable. Switches also have a default
value that is used by the switch when none of the values equal any of the suggested values in the switch statement. To invoke the Switch
command, you declare Switch ($variableToEvaluate)
. The second part of the Switch
command is to declare potential values that the $variableToEvaluate
could be, as shown here:
$x = "that" Switch ($x) { this { write-host "Value $x equals this." } that { write-host "Value $x equals that." } Default { write-host "Value Doesn't Match Any Other Value" } }
The output of this...