Introduction
As briefly mentioned in the blog post Mastering PowerShell’s Comparison Operators, PowerShell allows you to match patterns using different operators. In this post, I’ll dive deeper into the differences between two commonly used options: -like and -match. At first glance, they may seem interchangeable, but they’re not.
Before we get into the details of -like and -match, it’s worth noting that if you need an exact comparison, you’re better off using -eq.
-like
The -like operator is ideal for simple pattern matching. It’s useful when you know part of a string but not the whole thing. For example, you might want to find all function names that contain the word "active".
Example:
get-command | Where-Object Name -like "*Active*"
-like uses wildcards to match patterns. You can place these wildcards before, after, or around the word you want to match:
*= one or more arbitrary characters?= exactly one arbitrary character
-like is much more readable than -match, which makes it the preferred choice when regular expressions aren’t necessary.
-match
The -match operator uses regular expressions (regex) to match patterns. This allows for much more complex comparisons.
For example, to check if a string contains an email address:
"admin@example.com" -match "\w+@\w+\.\w+"
But regex goes beyond that, you can use it to extract specific parts of a string or filter log entries based on error codes.
With regex we can also use anchors to define if we want to check on the start and/or end of a string.
^matches the start of a string.$matches the end of a string.
Examples:
## Start with "Error"
"Error: File not found" -match "^Error" ## True
## Ends with a digit
"Version 2" -match "\d$" ## True
## Match exact string "OK"
"OK" -match "^OK$" ## True
But keep in mind that Powershell isn’t a CaseSensitive language. If you want to match on capitals, you need to force the checking on those with -match (CaseSentive Match).
Use -match only when regex is truly needed. Be sure to document and test your regex thoroughly. Tools like regex101.com make it easy to test and understand your expressions.
When Should You Use Which?
Now that we understand what each operator does, let’s look at when to use them. Here’s a quick reference:
| Use Case | Recommended Operator |
|---|---|
| Simple pattern matching | -like |
| Complex pattern recognition | -match |
| Input validation | -match |
| Filtering by filename | -like |
| Extracting data with regex | -match |
Quiz: Do You Know the Difference Between -match and -like?
Question 1: What’s the key difference between -match and -like?
A. -match uses wildcards, -like uses regex
B. -like uses wildcards, -match uses regex
C. Both use regex
D. Both use wildcards
Answer
✅ Correct answer: B
Question 2: Which of the following expressions correctly uses -like?
A. "PowerShell" -like "Power*"
B. "PowerShell" -like "^Power"
C. "PowerShell" -like "Power\S+"
D. "PowerShell" -like "PowerShell$"
Answer
✅ Correct answer: A
Question 3: Which of the following expressions correctly uses -match to check if a string starts with "Jos"?
A. "Jos Fissering" -match "Jos*"
B. "Jos Fissering" -match "^Jos"
C. "Jos Fissering" -match "Jos?"
D. "Jos Fissering" -match "*Jos"
Answer
✅ Correct answer: B
Question 4: What is the result of this expression?
"admin@example.com" -match "\w+@\w+\.\w+"
Answer
✅ Correct answer: True
Question 5: Which operator is best suited for filtering log entries that contain a specific error code like "ERR42"?
Answer
✅ Correct answer: -match
Bonus Question: You want to check if a string ends with .ps1. Which operator and pattern should you use?
Your answer:
Operator: __________
Pattern: __________
Answer
✅ Correct answer:
Operator: -match
Pattern: \.ps1$
Conclusion
-like is fast and simple, perfect for basic filtering. -match is powerful and flexible, but requires regex knowledge. By choosing the right operator for the task, you’ll make your PowerShell scripts more robust, readable, and maintainable.

Leave a Reply