The switch statement helps you check a value against many options easily. Using wildcards and regex lets you match patterns, not just exact words.
Switch with wildcard and regex in PowerShell
switch -Wildcard ($value) {
'pattern1' { # code }
'pattern2' { # code }
}
switch -Regex ($value) {
'regex1' { # code }
'regex2' { # code }
}-Wildcard lets you use * and ? to match parts of strings.
-Regex lets you use regular expressions for advanced pattern matching.
$word starts with 'cat', ends with 'dog', or contains 'bird'.switch -Wildcard ($word) {
'cat*' { "Starts with cat" }
'*dog' { "Ends with dog" }
'*bird*' { "Contains bird" }
}switch -Regex ($word) {
'^cat' { "Starts with cat" }
'dog$' { "Ends with dog" }
'bird' { "Contains bird" }
}This script checks the string 'catalog' with both wildcard and regex switches. The switch executes only the first matching case for each, printing the corresponding message.
$value = 'catalog' switch -Wildcard ($value) { 'cat*' { Write-Output "Matches wildcard: starts with 'cat'" } '*log' { Write-Output "Matches wildcard: ends with 'log'" } '*dog*' { Write-Output "Matches wildcard: contains 'dog'" } default { Write-Output "No wildcard match" } } switch -Regex ($value) { '^cat' { Write-Output "Matches regex: starts with 'cat'" } 'log$' { Write-Output "Matches regex: ends with 'log'" } 'dog' { Write-Output "Matches regex: contains 'dog'" } default { Write-Output "No regex match" } }
Use default to catch values that don't match any pattern.
Wildcards are simpler but less powerful than regex.
Regex patterns can be tricky; test them carefully.
switch with -Wildcard matches simple patterns with * and ?.
switch with -Regex matches complex patterns using regular expressions.
Use these to write cleaner, easier-to-read code when checking many patterns.
Practice
-Wildcard parameter do in a PowerShell switch statement?Solution
Step 1: Understand the role of -Wildcard
The-Wildcardparameter allows pattern matching using simple wildcards such as * (any characters) and ? (single character).Step 2: Differentiate from -Regex
Unlike-Regex, which uses complex regular expressions,-Wildcardis simpler and easier for basic pattern matching.Final Answer:
It matches patterns using simple wildcards like * and ?. -> Option DQuick Check:
-Wildcard = simple wildcard matching [OK]
- Confusing -Wildcard with -Regex
- Thinking -Wildcard ignores case by default
- Assuming -Wildcard speeds up execution
switch statement with wildcard matching in PowerShell?Solution
Step 1: Identify correct parameter placement
In PowerShell, the-Wildcardparameter is placed immediately afterswitchand before the input expression.Step 2: Check the pattern and block syntax
The pattern '*test*' and the script block '{ 'Match' }' are correctly placed inside the switch braces.Final Answer:
switch -Wildcard ($input) { '*test*' { 'Match' } } -> Option CQuick Check:
Parameter right after switch keyword = switch -Wildcard ($input) { '*test*' { 'Match' } } [OK]
- Placing -Wildcard inside the switch block
- Putting -Wildcard after the input variable
- Using -Wildcard as a pattern inside braces
switch -Regex ('apple', 'banana', 'cherry') {
'^a' { 'Starts with a' }
'an' { 'Contains an' }
default { 'No match' }
}Solution
Step 1: Check each input against regex patterns
'apple' matches '^a' (starts with 'a'), so outputs 'Starts with a'. 'banana' contains 'an', so matches 'an' pattern, outputs 'Contains an'. 'cherry' matches none, so outputs 'No match'.Step 2: Confirm output order
The outputs correspond to each input in order: 'apple' -> 'Starts with a', 'banana' -> 'Contains an', 'cherry' -> 'No match'.Final Answer:
Starts with a Contains an No match -> Option AQuick Check:
Regex matches each input correctly = Starts with a Contains an No match [OK]
- Assuming only first match per input
- Confusing regex anchors like ^
- Ignoring default case output
switch statement using wildcard matching:switch ($word) -Wildcard {
'he*' { 'Starts with he' }
'*lo' { 'Ends with lo' }
default { 'No match' }
}Solution
Step 1: Check parameter placement
The-Wildcardparameter must come immediately after theswitchkeyword, not after the input variable.Step 2: Validate patterns and default block
The patterns 'he*' and '*lo' are valid wildcards. The default case has a proper script block. Using wildcards with variables is allowed.Final Answer:
The -Wildcard parameter should be before the input variable. -> Option AQuick Check:
Parameter placement error = The -Wildcard parameter should be before the input variable. [OK]
- Putting -Wildcard after input variable
- Thinking wildcards are invalid patterns
- Omitting script block braces
switch with both wildcard and regex patterns. Which approach correctly combines -Wildcard and -Regex in one script to handle different patterns?Solution
Step 1: Understand parameter exclusivity
PowerShell'sswitchdoes not support using-Wildcardand-Regextogether in one statement. They are mutually exclusive parameters.Step 2: Choose practical approach
To handle both wildcard and regex patterns, use two separateswitchstatements: one with-Wildcardand one with-Regex, processing the list twice.Final Answer:
Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. -> Option BQuick Check:
Separate switches for wildcard and regex = Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. [OK]
- Trying to combine -Wildcard and -Regex in one switch
- Assuming pattern modifiers inside switch block
- Converting wildcards manually instead of separate switches
Ready to prove it?
Pass the test and this pattern moves unknown
