0
0
PowerShellscripting

Switch with wildcard and regex in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the -Wildcard parameter do in a PowerShell switch statement?
It allows the switch statement to match input strings using wildcard patterns like * and ?, similar to file name matching.
Click to reveal answer
beginner
How do you enable regex matching in a PowerShell switch statement?
Use the -Regex parameter with switch. This makes the statement treat each case as a regular expression pattern.
Click to reveal answer
intermediate
What is the difference between -Wildcard and -Regex in a PowerShell switch?
-Wildcard uses simple wildcard patterns like * and ?. -Regex uses full regular expressions, which are more powerful and flexible but more complex.
Click to reveal answer
beginner
Write a simple PowerShell switch statement using -Wildcard to match strings ending with '.txt'.
switch -Wildcard ($file) { '*.txt' { "Text file found: $file" } default { "Other file: $file" } }
Click to reveal answer
intermediate
Explain why you might choose -Regex over -Wildcard in a switch statement.
You choose -Regex when you need complex pattern matching, like matching specific character sets, repetitions, or positions, which wildcards cannot handle.
Click to reveal answer
Which parameter enables wildcard pattern matching in a PowerShell switch statement?
A-Exact
B-Regex
C-Pattern
D-Wildcard
What does the -Regex parameter do in a PowerShell switch statement?
AMatches input using regular expressions
BMatches exact strings only
CIgnores case sensitivity
DMatches only numbers
Which of these is a valid wildcard pattern to match any string ending with '.log'?
A.*\.log
B*.log
Clog*
D?log
In a switch statement with -Regex, which pattern matches strings starting with 'Error'?
A?Error
B*Error
C^Error
DError$
What happens if no case matches in a PowerShell switch statement?
AThe default block runs if defined
BThe script throws an error
CThe switch repeats automatically
DThe script exits immediately
Describe how to use the switch statement in PowerShell with wildcard matching. Include an example.
Think about matching file extensions or simple patterns.
You got /3 concepts.
    Explain the benefits of using -Regex in a PowerShell switch statement compared to -Wildcard.
    Consider pattern complexity and flexibility.
    You got /3 concepts.