Tutorial by Examples

Switch statements compare a single test value to multiple conditions, and performs any associated actions for successful comparisons. It can result in multiple matches/actions. Given the following switch... switch($myValue) { 'First Condition' { 'First Action' } 'Second Condition' ...
The -Regex parameter allows switch statements to perform regular expression matching against conditions. Example: switch -Regex ('Condition') { 'Con\D+ion' {'One or more non-digits'} 'Conditio*$' {'Zero or more "o"'} 'C.ndition' {'Any single char.'} '^C\w+ition$'...
The break keyword can be used in switch statements to exit the statement before evaluating all conditions. Example: switch('Condition') { 'Condition' { 'First Action' } 'Condition' { 'Second Action' break } 'Condition' { 'Third Action' } } Output...
The -Wildcard parameter allows switch statements to perform wildcard matching against conditions. Example: switch -Wildcard ('Condition') { 'Condition' {'Normal match'} 'Condit*' {'Zero or more wildcard chars.'} 'C[aoc]ndit[f-l]on' {'Range and set of chars...
The -Exact parameter enforces switch statements to perform exact, case-insensitive matching against string-conditions. Example: switch -Exact ('Condition') { 'condition' {'First Action'} 'Condition' {'Second Action'} 'conditioN' {'Third Action'} '^*ondition$' {'Fourth Action...
The -CaseSensitive parameter enforces switch statements to perform exact, case-sensitive matching against conditions. Example: switch -CaseSensitive ('Condition') { 'condition' {'First Action'} 'Condition' {'Second Action'} 'conditioN' {'Third Action'} } Output: Second Act...
The -file parameter allows the switch statement to receive input from a file. Each line of the file is evaluated by the switch statement. Example file input.txt: condition test Example switch statement: switch -file input.txt { 'condition' {'First Action'} 'test' {'Second Action...
The Default keyword is used to execute an action when no other conditions match the input value. Example: switch('Condition') { 'Skip Condition' { 'First Action' } 'Skip This Condition Too' { 'Second Action' } Default { 'Default Action' } } Output: D...
Conditions can also be expressions: $myInput = 0 switch($myInput) { # because the result of the expression, 4, # does not equal our input this block should not be run. (2+2) { 'True. 2 +2 = 4' } # because the result of the expression, 0, # does equal our input this ...

Page 1 of 1