0
0
PowerShellscripting~20 mins

match operator in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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' }
}
AFound PowerShell
BPowerShell 7.2
CNo match
DFound version number
Attempts:
2 left
💡 Hint
The switch statement with -Regex checks patterns in order and stops at the first match.
💻 Command Output
intermediate
1: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'
Atrue
Bworld
CHello World
Dfalse
Attempts:
2 left
💡 Hint
The match operator is case-sensitive by default.
📝 Syntax
advanced
2: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'.
A'abc123def456' -match '\d+'
B'abc123def456' -match '\d+' -AllMatches
C'abc123def456' -match '\d+' -AllMatches | ForEach-Object { $_.Matches }
D'abc123def456' -match '\d+' | Select-String -AllMatches
Attempts:
2 left
💡 Hint
The -match operator returns a boolean by default; to get all matches, use the $Matches automatic variable with -AllMatches.
🔧 Debug
advanced
2: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
ABecause the string variable is not properly assigned
BBecause the match operator is case-sensitive and 'test' does not match 'Test'
CBecause the match operator requires the pattern to be in double quotes
DBecause the match operator only works with arrays
Attempts:
2 left
💡 Hint
Check if the match operator is case-sensitive by default.
🚀 Application
expert
3: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?
A$string = 'An apple a day keeps the doctor away'; $string -match '\b[aA]\w*\b' -AllMatches; $Matches.Matches | ForEach-Object { $_.Value }
B$matches = 'An apple a day keeps the doctor away' -match '\b[aA]\w*\b' -AllMatches; $matches.Matches.Value
C'An apple a day keeps the doctor away' -match '\b[aA]\w*\b' -AllMatches | ForEach-Object { $_.Matches.Value }
D'An apple a day keeps the doctor away' -imatch '\b[a]\w*\b' | Select-Object -ExpandProperty Matches
Attempts:
2 left
💡 Hint
Remember that -match returns a boolean, but with -AllMatches you can access $Matches automatic variable.