PowerShell Switch statement Switch Statement with Exact Parameter

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any PowerShell Question?