Bird
Raised Fist0
PowerShellscripting~10 mins

String comparison (-like, -match) in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String comparison (-like, -match)
Input: String, Pattern
↓
Choose Operator
↓
Wildcard?
↓
Compare using
↓
Wildcard match
↓
Return True/False
↓
Output
The flow shows how PowerShell compares strings using -like (wildcards) or -match (regex) and returns True or False.
Execution Sample
PowerShell
$str = 'Hello World'
$str -like 'H*o W*'
$str -match '^Hello\sWorld$'
This code compares the string 'Hello World' using -like with wildcards and -match with regex.
Execution Table
StepExpressionEvaluationResult
1'Hello World' -like 'H*o W*'Does 'Hello World' fit pattern 'H*o W*'?True
2'Hello World' -match '^Hello\sWorld$'Does 'Hello World' match regex '^Hello\sWorld$'?True
3'Hello World' -like 'h*o w*'Case insensitive? Yes, so pattern 'h*o w*' matches?True
4'Hello World' -match 'world$'Regex match 'world$' case sensitive?False
5'Hello World' -match '(?i)world$'Regex match 'world$' with case-insensitive flag?True
💡 All comparisons done, showing True or False results based on pattern and case sensitivity.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
$str'Hello World''Hello World''Hello World''Hello World''Hello World''Hello World'
Key Moments - 3 Insights
Why does '-like' comparison with lowercase pattern succeed on 'Hello World'?
Because '-like' is case-insensitive by default, so the pattern 'h*o w*' matches regardless of case, returns True as shown in step 3.
Why does '-match' fail when matching 'world$' without case-insensitive flag?
Because '-match' is case-sensitive by default, so 'world$' does not match 'World' in 'Hello World' as shown in step 4.
How can '-match' be made case-insensitive?
By adding '(?i)' at the start of the regex pattern, making it case-insensitive, as shown in step 5 where the match returns True.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of 'Hello World' -like 'H*o W*' at step 1?
AFalse
BError
CTrue
DNull
💡 Hint
Check the 'Result' column in row for step 1 in the execution_table.
At which step does the '-match' operator return True with case-insensitive regex?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look for '(?i)' in the regex pattern in the execution_table rows.
If we change the pattern in step 3 to 'H*o W*', what would be the result?
ATrue
BFalse
CError
DNull
💡 Hint
Compare step 1 and step 3 patterns and their results in the execution_table.
Concept Snapshot
PowerShell string comparison:
-like uses wildcards (*, ?), case-insensitive by default.
-match uses regex, case-sensitive by default.
Use '(?i)' in regex for case-insensitive match.
Returns True or False based on pattern match.
Full Transcript
This lesson shows how PowerShell compares strings using two operators: -like and -match. The -like operator uses simple wildcard patterns like * and ? and is case-insensitive by default. The -match operator uses regular expressions and is case-sensitive by default. We see examples comparing the string 'Hello World' with patterns using both operators. The execution table traces each comparison step, showing whether the string matches the pattern or not. Key points include understanding case sensitivity differences and how to make regex matches case-insensitive by adding '(?i)'. The visual quiz tests understanding of these behaviors by asking about specific steps and results. This helps beginners see exactly how string comparison works in PowerShell.

Practice

(1/5)
1. Which operator in PowerShell uses simple wildcard patterns for string comparison?
easy
A. -contains
B. -like
C. -match
D. -eq

Solution

  1. Step 1: Understand the purpose of -like

    The -like operator uses simple wildcard characters like * and ? to match patterns in strings.
  2. Step 2: Compare with -match

    The -match operator uses regular expressions, which are more complex than simple wildcards.
  3. Final Answer:

    -like -> Option B
  4. Quick Check:

    Simple wildcard matching = -like [OK]
Hint: Remember: -like = wildcards, -match = regex [OK]
Common Mistakes:
  • Confusing -like with -match
  • Thinking -contains works like -like
  • Assuming -eq supports wildcards
2. Which of the following is the correct syntax to check if the variable $name contains the substring 'John' using -like?
easy
A. $name -like 'John*'
B. $name -like 'John?'
C. $name -like '*John*'
D. $name -like '?John?'

Solution

  1. Step 1: Understand wildcard usage with -like

    The asterisk (*) wildcard matches zero or more characters. To check if 'John' appears anywhere, use '*John*'.
  2. Step 2: Analyze each option

    $name -like 'John*' checks if string starts with 'John'. $name -like '*John*' checks if 'John' is anywhere. $name -like '?John?' uses '?' which matches exactly one character, so it won't match 'John' alone. $name -like 'John?' checks if string starts with 'John' and has one more character.
  3. Final Answer:

    $name -like '*John*' -> Option C
  4. Quick Check:

    Substring anywhere = '*John*' [OK]
Hint: Use * before and after substring to match anywhere [OK]
Common Mistakes:
  • Forgetting to use * wildcards around substring
  • Using ? instead of * for multiple characters
  • Assuming -like matches exact substring without wildcards
3. What will be the output of this PowerShell code?
$text = 'PowerShell123'
if ($text -match '\d{3}$') { 'Ends with 3 digits' } else { 'No match' }
medium
A. Ends with 3 digits
B. No match
C. Syntax error
D. Empty output

Solution

  1. Step 1: Understand the regex pattern '\d{3}$'

    This pattern matches exactly three digits (\d{3}) at the end of the string ($).
  2. Step 2: Check the string 'PowerShell123'

    The string ends with '123', which is three digits, so the pattern matches.
  3. Final Answer:

    Ends with 3 digits -> Option A
  4. Quick Check:

    Regex matches end digits = 'Ends with 3 digits' [OK]
Hint: Use -match with regex to find patterns like digits [OK]
Common Mistakes:
  • Confusing -like with regex patterns
  • Not escaping backslash in regex
  • Assuming -match returns boolean instead of string output
4. Identify the error in this PowerShell snippet:
$file = 'report.doc'
if ($file -clike '*.DOC') { 'Match found' } else { 'No match' }
medium
A. -clike is case-sensitive, so '*.DOC' won't match 'report.doc'
B. Wildcard * is missing
C. Quotes around pattern are incorrect
D. Variable $file is not defined

Solution

  1. Step 1: Understand the -clike operator

    -clike is the case-sensitive counterpart to -like. It requires exact case matching for the pattern.
  2. Step 2: Analyze the mismatch

    The string 'report.doc' ends with lowercase '.doc', but the pattern '*.DOC' specifies uppercase '.DOC'. Other options are invalid: * is present, quotes are correct, $file is defined.
  3. Final Answer:

    -clike is case-sensitive, so '*.DOC' won't match 'report.doc' -> Option A
  4. Quick Check:

    Case mismatch with -clike = no match [OK]
Hint: Remember: -clike = case-sensitive, -like = case-insensitive [OK]
Common Mistakes:
  • Forgetting -clike is case-sensitive
  • Thinking wildcards are missing
  • Misunderstanding variable initialization
5. You want to filter a list of filenames to include only those that start with 'log' and end with a 4-digit year using PowerShell. Which command correctly uses -match to do this?
$files = @('log2020.txt', 'log2019.csv', 'data2020.log', 'log20a0.txt')
hard
A. $files | Where-Object { $_ -like 'log????' }
B. $files | Where-Object { $_ -like 'log*2020' }
C. $files | Where-Object { $_ -match 'log\d{4}$' }
D. $files | Where-Object { $_ -match '^log\d{4}' }

Solution

  1. Step 1: Understand the pattern requirements

    We want filenames starting with 'log' (^log) followed immediately by exactly 4 digits (\d{4}).
  2. Step 2: Analyze each option

    $files | Where-Object { $_ -match '^log\d{4}' } uses regex '^log\d{4}' which matches strings starting with 'log' plus 4 digits. $files | Where-Object { $_ -like 'log????' } uses -like with 'log????' which matches 'log' plus any 4 characters but not necessarily digits. $files | Where-Object { $_ -match 'log\d{4}$' } matches 'log' followed by 4 digits at the end, but the year is not necessarily at the end of the filename. $files | Where-Object { $_ -like 'log*2020' } matches 'log' followed by anything and ending with '2020', which is too specific.
  3. Final Answer:

    $files | Where-Object { $_ -match '^log\d{4}' } -> Option D
  4. Quick Check:

    Start with 'log' + 4 digits = $files | Where-Object { $_ -match '^log\d{4}' } [OK]
Hint: Use ^ for start and \d{4} for 4 digits in regex [OK]
Common Mistakes:
  • Using -like with ? instead of digits
  • Matching digits at end instead of start
  • Confusing -like and -match patterns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard