Bird
Raised Fist0
PowerShellscripting~20 mins

Switch with wildcard and regex in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Switch with Wildcard Matching
What is the output of this PowerShell script using switch with wildcard matching?
PowerShell
$input = "file123.txt"

switch -Wildcard ($input) {
  "file*" {"Matched file pattern"}
  "data*" {"Matched data pattern"}
  default {"No match"}
}
ANo match
BMatched file pattern
Cfile123.txt
DMatched data pattern
Attempts:
2 left
💡 Hint
Remember that -Wildcard matches patterns with * as a wildcard.
💻 Command Output
intermediate
2:00remaining
Switch with Regex Matching Output
What will this PowerShell switch statement output when using regex matching?
PowerShell
$input = "123-45-6789"

switch -Regex ($input) {
  '^\d{3}-\d{2}-\d{4}$' {"SSN format detected"}
  '^\w+@\w+\.com$' {"Email format detected"}
  default {"Unknown format"}
}
AUnknown format
BEmail format detected
CSSN format detected
D123-45-6789
Attempts:
2 left
💡 Hint
Look at the regex patterns and see which matches the input string.
🔧 Debug
advanced
2:00remaining
Identify the Error in Switch with Wildcard
This PowerShell script is intended to match inputs starting with 'test' using wildcard in switch, but it throws an error. What is the cause?
PowerShell
$input = "testing123"

switch ($input) {
  -Wildcard "test*" {"Starts with test"}
  default {"No match"}
}
AThe switch statement requires -Regex parameter for wildcard matching
BThe wildcard pattern must be in single quotes, not double quotes
CThe input variable must be declared before the switch statement
DThe -Wildcard parameter must be after the switch keyword, not inside the block
Attempts:
2 left
💡 Hint
Check where the -Wildcard parameter is placed in the switch syntax.
🚀 Application
advanced
3:00remaining
Using Switch with Mixed Wildcard and Regex
You want to write a PowerShell switch statement that matches inputs starting with 'log' using wildcard and inputs matching a date format using regex. Which code snippet correctly implements this?
A
switch ($input) {
  {$_ -like "log*"} {"Log file"}
  {$_ -match '^\d{4}-\d{2}-\d{2}$'} {"Date format"}
  default {"No match"}
}
B
switch ($input) {
  -Wildcard "log*" {"Log file"}
  -Regex '^\d{4}-\d{2}-\d{2}$' {"Date format"}
  default {"No match"}
}
C
switch -Wildcard ($input) {
  "log*" {"Log file"}
  -Regex '^\d{4}-\d{2}-\d{2}$' {"Date format"}
  default {"No match"}
}
D
switch -Wildcard -Regex ($input) {
  "log*" {"Log file"}
  '^\d{4}-\d{2}-\d{2}$' {"Date format"}
  default {"No match"}
}
Attempts:
2 left
💡 Hint
PowerShell switch can only use one matching mode at a time; use script blocks for mixed matching.
🧠 Conceptual
expert
3:00remaining
Behavior of Switch with Wildcard and Regex on Multiple Matches
In PowerShell, when using switch with the -Wildcard parameter, what happens if multiple patterns match the input? How does this differ from using -Regex with multiple matches?
ABoth -Wildcard and -Regex run all matching cases
BWith -Wildcard, all matching cases run; with -Regex, only the first matching case runs
CBoth -Wildcard and -Regex run only the first matching case
DWith -Wildcard, only the first matching case runs; with -Regex, all matching cases run
Attempts:
2 left
💡 Hint
Think about how switch processes multiple matches in PowerShell.

Practice

(1/5)
1. What does the -Wildcard parameter do in a PowerShell switch statement?
easy
A. It matches patterns using complex regular expressions.
B. It makes the switch statement run faster by skipping checks.
C. It forces the switch to ignore case sensitivity.
D. It matches patterns using simple wildcards like * and ?.

Solution

  1. Step 1: Understand the role of -Wildcard

    The -Wildcard parameter allows pattern matching using simple wildcards such as * (any characters) and ? (single character).
  2. Step 2: Differentiate from -Regex

    Unlike -Regex, which uses complex regular expressions, -Wildcard is simpler and easier for basic pattern matching.
  3. Final Answer:

    It matches patterns using simple wildcards like * and ?. -> Option D
  4. Quick Check:

    -Wildcard = simple wildcard matching [OK]
Hint: Remember: Wildcard = * and ? patterns, Regex = complex patterns [OK]
Common Mistakes:
  • Confusing -Wildcard with -Regex
  • Thinking -Wildcard ignores case by default
  • Assuming -Wildcard speeds up execution
2. Which of the following is the correct syntax to use a switch statement with wildcard matching in PowerShell?
easy
A. switch ($input) { '*test*' -Wildcard { 'Match' } }
B. switch ($input) -Wildcard { '*test*' { 'Match' } }
C. switch -Wildcard ($input) { '*test*' { 'Match' } }
D. switch ($input) { -Wildcard '*test*' { 'Match' } }

Solution

  1. Step 1: Identify correct parameter placement

    In PowerShell, the -Wildcard parameter is placed immediately after switch and before the input expression.
  2. Step 2: Check the pattern and block syntax

    The pattern '*test*' and the script block '{ 'Match' }' are correctly placed inside the switch braces.
  3. Final Answer:

    switch -Wildcard ($input) { '*test*' { 'Match' } } -> Option C
  4. Quick Check:

    Parameter right after switch keyword = switch -Wildcard ($input) { '*test*' { 'Match' } } [OK]
Hint: Put -Wildcard right after switch, before input [OK]
Common Mistakes:
  • Placing -Wildcard inside the switch block
  • Putting -Wildcard after the input variable
  • Using -Wildcard as a pattern inside braces
3. What will be the output of this PowerShell code?
switch -Regex ('apple', 'banana', 'cherry') {
  '^a' { 'Starts with a' }
  'an' { 'Contains an' }
  default { 'No match' }
}
medium
A. Starts with a Contains an No match
B. Starts with a No match No match
C. No match Contains an No match
D. Starts with a Contains an Contains an

Solution

  1. 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'.
  2. Step 2: Confirm output order

    The outputs correspond to each input in order: 'apple' -> 'Starts with a', 'banana' -> 'Contains an', 'cherry' -> 'No match'.
  3. Final Answer:

    Starts with a Contains an No match -> Option A
  4. Quick Check:

    Regex matches each input correctly = Starts with a Contains an No match [OK]
Hint: Match each input line by line with regex patterns [OK]
Common Mistakes:
  • Assuming only first match per input
  • Confusing regex anchors like ^
  • Ignoring default case output
4. Identify the error in this PowerShell switch statement using wildcard matching:
switch ($word) -Wildcard {
  'he*' { 'Starts with he' }
  '*lo' { 'Ends with lo' }
  default { 'No match' }
}
medium
A. The -Wildcard parameter should be before the input variable.
B. The patterns 'he*' and '*lo' are invalid wildcards.
C. The default case is missing a script block.
D. The switch statement cannot use wildcards with variables.

Solution

  1. Step 1: Check parameter placement

    The -Wildcard parameter must come immediately after the switch keyword, not after the input variable.
  2. 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.
  3. Final Answer:

    The -Wildcard parameter should be before the input variable. -> Option A
  4. Quick Check:

    Parameter placement error = The -Wildcard parameter should be before the input variable. [OK]
Hint: Place -Wildcard right after switch, before input variable [OK]
Common Mistakes:
  • Putting -Wildcard after input variable
  • Thinking wildcards are invalid patterns
  • Omitting script block braces
5. You want to categorize a list of filenames using switch with both wildcard and regex patterns. Which approach correctly combines -Wildcard and -Regex in one script to handle different patterns?
hard
A. Use a single switch with both -Wildcard and -Regex parameters together.
B. Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice.
C. Use -Wildcard and -Regex as pattern modifiers inside one switch statement.
D. Use -Regex only and convert wildcards to regex patterns manually.

Solution

  1. Step 1: Understand parameter exclusivity

    PowerShell's switch does not support using -Wildcard and -Regex together in one statement. They are mutually exclusive parameters.
  2. Step 2: Choose practical approach

    To handle both wildcard and regex patterns, use two separate switch statements: one with -Wildcard and one with -Regex, processing the list twice.
  3. Final Answer:

    Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. -> Option B
  4. Quick Check:

    Separate switches for wildcard and regex = Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. [OK]
Hint: Use separate switches for wildcard and regex patterns [OK]
Common Mistakes:
  • Trying to combine -Wildcard and -Regex in one switch
  • Assuming pattern modifiers inside switch block
  • Converting wildcards manually instead of separate switches
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard