This code compares the string 'Hello World' using -like with wildcards and -match with regex.
Execution Table
Step
Expression
Evaluation
Result
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
Variable
Start
After Step 1
After Step 2
After Step 3
After Step 4
After 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.