The -like operator checks if a string matches a pattern using wildcard characters like * and ?. It returns True if the pattern matches part or all of the string.
Click to reveal answer
intermediate
How does the -match operator differ from -like in PowerShell?
-match uses regular expressions to compare strings, allowing more complex pattern matching than -like, which uses simple wildcards.
Click to reveal answer
beginner
Example: What is the result of 'hello' -like 'h*o'?
It returns True because the pattern h*o matches any string starting with 'h' and ending with 'o', including 'hello'.
Click to reveal answer
intermediate
Example: What does 'hello' -match '^h.*o$' return?
It returns True because the regular expression ^h.*o$ means the string starts with 'h' and ends with 'o', matching 'hello'.
Click to reveal answer
advanced
Can -match capture parts of a string? How?
Yes, -match can capture parts using parentheses in the regex. Captured groups are stored in the automatic variable $matches.
Click to reveal answer
Which operator uses wildcard characters like * and ? for string comparison in PowerShell?
A-match
B-contains
C-eq
D-like
✗ Incorrect
-like uses wildcards like * and ? for simple pattern matching.
What does the -match operator use to compare strings?
ARegular expressions
BExact equality
CWildcards
DNumeric comparison
✗ Incorrect
-match uses regular expressions for flexible pattern matching.
What will 'PowerShell' -like 'P*Shell' return?
AFalse
BTrue
CError
DNull
✗ Incorrect
The pattern matches because it starts with 'P' and ends with 'Shell'.
Which variable holds captured groups after a successful -match operation?
A$matches
B$groups
C$result
D$capture
✗ Incorrect
Captured groups are stored in the automatic variable $matches.
Which operator would you use to check if a string exactly equals another string?
A-like
B-match
C-eq
D-contains
✗ Incorrect
-eq checks for exact equality, not pattern matching.
Explain the difference between the PowerShell operators -like and -match.
Think about simple vs complex pattern matching.
You got /4 concepts.
Describe how to capture parts of a string using the -match operator in PowerShell.
Remember $matches is automatic after -match.
You got /3 concepts.
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
Step 1: Understand the purpose of -like
The -like operator uses simple wildcard characters like * and ? to match patterns in strings.
Step 2: Compare with -match
The -match operator uses regular expressions, which are more complex than simple 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
Step 1: Understand wildcard usage with -like
The asterisk (*) wildcard matches zero or more characters. To check if 'John' appears anywhere, use '*John*'.
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.
Final Answer:
$name -like '*John*' -> Option C
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
Step 1: Understand the regex pattern '\d{3}$'
This pattern matches exactly three digits (\d{3}) at the end of the string ($).
Step 2: Check the string 'PowerShell123'
The string ends with '123', which is three digits, so the pattern matches.
Final Answer:
Ends with 3 digits -> Option A
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
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
Step 1: Understand the -clike operator
-clike is the case-sensitive counterpart to -like. It requires exact case matching for the pattern.
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.
Final Answer:
-clike is case-sensitive, so '*.DOC' won't match 'report.doc' -> Option A
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?
C. $files | Where-Object { $_ -match 'log\d{4}$' }
D. $files | Where-Object { $_ -match '^log\d{4}' }
Solution
Step 1: Understand the pattern requirements
We want filenames starting with 'log' (^log) followed immediately by exactly 4 digits (\d{4}).
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.