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: ...

Get Mastering Windows PowerShell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.