Challenge - 5 Problems
PowerShell Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell match operator example?
Consider the following PowerShell code snippet using the match operator. What will be the output?
PowerShell
switch -Regex ($input = 'PowerShell 7.2') { 'PowerShell' { 'Found PowerShell' } '\d+\.\d+' { 'Found version number' } default { 'No match' } }
Attempts:
2 left
💡 Hint
The switch statement with -Regex checks patterns in order and stops at the first match.
✗ Incorrect
The switch statement with -Regex checks each pattern in order. The first pattern 'PowerShell' matches the input string, so it outputs 'Found PowerShell' and stops checking further patterns.
💻 Command Output
intermediate1:30remaining
What does this PowerShell match operator return?
What is the output of this PowerShell command using the match operator?
PowerShell
'Hello World' -match 'world'
Attempts:
2 left
💡 Hint
The match operator is case-sensitive by default.
✗ Incorrect
The match operator is case-sensitive, so 'world' does not match 'World'. Therefore, the result is false.
📝 Syntax
advanced2:30remaining
Which option correctly uses the match operator to find all digits in a string?
Select the PowerShell command that correctly uses the match operator to extract all digits from the string 'abc123def456'.
Attempts:
2 left
💡 Hint
The -match operator returns a boolean by default; to get all matches, use the $Matches automatic variable with -AllMatches.
✗ Incorrect
Option C correctly uses -match with -AllMatches and processes the Matches collection to extract all digit sequences. Option C returns only a boolean. Option C is invalid syntax. Option C misuses Select-String.
🔧 Debug
advanced2:00remaining
Why does this PowerShell match operator code fail to find a match?
Given the code below, why does the match operator fail to find 'test' in the string?
PowerShell
$string = 'This is a Test string' $result = $string -match 'test' $result
Attempts:
2 left
💡 Hint
Check if the match operator is case-sensitive by default.
✗ Incorrect
The match operator is case-sensitive by default, so 'test' does not match 'Test'. To match ignoring case, use -imatch instead.
🚀 Application
expert3:00remaining
How to extract all words starting with 'a' or 'A' using PowerShell match operator?
You want to extract all words from the string 'An apple a day keeps the doctor away' that start with 'a' or 'A'. Which PowerShell code correctly does this?
Attempts:
2 left
💡 Hint
Remember that -match returns a boolean, but with -AllMatches you can access $Matches automatic variable.
✗ Incorrect
Option A correctly uses -match with -AllMatches on the string variable, then accesses the automatic $Matches variable's Matches collection to extract all matching words. Option A tries to pipe the boolean result, which fails. Option A assigns the boolean to $matches, not the automatic variable. Option A misuses -imatch and Select-Object.