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
✗ Incorrect
The -Wildcard parameter allows matching using wildcard patterns like * and ?.
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
✗ Incorrect
The -Regex parameter treats each case as a regular expression pattern for matching.
Which of these is a valid wildcard pattern to match any string ending with '.log'?
A.*\.log
B*.log
Clog*
D?log
✗ Incorrect
The pattern *.log matches any string ending with '.log' using wildcards.
In a switch statement with -Regex, which pattern matches strings starting with 'Error'?
A?Error
B*Error
C^Error
DError$
✗ Incorrect
The regex pattern ^Error matches strings that start with 'Error'.
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
✗ Incorrect
If no case matches, the default block runs if it is defined.
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.
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
Step 1: Understand the role of -Wildcard
The -Wildcard parameter 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, -Wildcard is simpler and easier for basic pattern matching.
Final Answer:
It matches patterns using simple wildcards like * and ?. -> Option D
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
Step 1: Check parameter placement
The -Wildcard parameter must come immediately after the switch keyword, 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 A
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
Step 1: Understand parameter exclusivity
PowerShell's switch does not support using -Wildcard and -Regex together in one statement. They are mutually exclusive parameters.
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.
Final Answer:
Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.