Switch

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 an 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 an pear"; break}
   "orange" {"We found an orange"; break}
   "peach"  {"We found an peach"; break}
   "banana" {"We found an 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 an 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

"History teaches us that men and nations behave wisely once they have exhausted all other alternatives" ~ Abba Eban

Related PowerShell Commands:

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.


 
Copyright © SS64.com 1999-2019
Some rights reserved