PowerShell Switch Statement with Exact Parameter


Example

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'} 
  'Conditio*'   {'Fifth Action'} 
}

Output:

First Action
Second Action
Third Action

The first through third actions are executed because their associated conditions matched the input. The regex and wildcard strings in the fourth and fifth conditions fail matching.

Note that the fourth condition would also match the input string if regular expression matching was being performed, but was ignored in this case because it is not.