Bird
0
0

Identify the error in this PowerShell code snippet:

medium📝 Debug Q6 of 15
PowerShell - Operators
Identify the error in this PowerShell code snippet:
$str = 'PowerShell'
if ($str -like '[Pp]ower*') { 'Yes' }
ANo error, code works correctly
BThe wildcard * is missing
CThe pattern should be in double quotes
DSquare brackets are invalid in -like patterns
Step-by-Step Solution
Solution:
  1. Step 1: Understand -like pattern limitations

    The -like operator supports wildcards but does not support character classes like [Pp].
  2. Step 2: Identify correct usage

    Character classes are valid in regex with -match, but not in -like. So the pattern is invalid for -like.
  3. Final Answer:

    Square brackets are invalid in -like patterns -> Option D
  4. Quick Check:

    Character classes need -match, not -like [OK]
Quick Trick: Use -match for regex features like [] [OK]
Common Mistakes:
  • Using regex syntax with -like
  • Assuming * is missing
  • Thinking quotes cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes