0
0
PowerShellscripting~20 mins

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

Choose your learning style9 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.