Comparison operators and filtering
Compare with -eq, -like, -match, -contains and friends, combine tests, and avoid the classic traps.
- Use PowerShell’s comparison operators, including case-sensitive variants
- Match text with -like wildcards and -match regular expressions
- Combine conditions, and predict how operators behave on collections and $null
PowerShell’s comparison operators are words, not symbols - > is already taken for redirecting output to files:
| Operator | Means | Example (all true) |
|---|---|---|
-eq / -ne | equal / not equal | 'Ember' -eq 'ember' |
-gt -ge -lt -le | greater / less (or equal) | 15 -gt 12 |
-like / -notlike | wildcard match: * any text, ? one character | 'Thornjaw' -like 'thorn*' |
-match / -notmatch | regular expression match | 'DRG-0042' -match '^DRG-\d{4}$' |
-contains / -notcontains | collection contains a value | @('Wyrm', 'Wisp') -contains 'Wisp' |
-in / -notin | value is in a collection | 'Wisp' -in 'Wyrm', 'Wisp' |
-is / -isnot | type check | 42 -is [int] |
String comparisons ignore case by default. Put a c in front for case-sensitive versions: -ceq, -clike, -cmatch. Combine tests with -and, -or and -not (or !).
Try it
True or false?
Predict what each expression returns.
“'Ember' -eq 'EMBER'”
“'Ember' -ceq 'EMBER'”
“'Skyla' -like 'sky*'”
“'Skyla' -like 'sky'”
“'Skyla' -match 'ky'”
“'10' -gt '9'”
“10 -gt '9'”
“'Wisp' -in 'Wyrm', 'Wisp'”
“-not (3 -gt 2)”
-match and $Matches
When -match succeeds on a single string, PowerShell fills the automatic $Matches hashtable: $Matches[0] is the whole match and $Matches[1], $Matches[2]... are the parenthesized groups. Name a group with (?<name>...) and read it as $Matches.name.
1$tag = 'DRG-0042: Ember (Firedrake)'
2if ($tag -match '^DRG-(\d+): (?<name>\w+) \((?<species>\w+)\)$') {
3 "id $([int]$Matches[1]), name $($Matches.name), species $($Matches.species)"
4}
5'Glim' -match '^[aeiou]'
6'Ember', 'Glim', 'Oswin' -match '^[aeiou]'id 42, name Ember, species Firedrake False Ember Oswin
Look at the last line: with a collection on the left, comparison operators don’t return true or false - they filter, returning the items that pass. That’s handy ($weights -gt 100 gives the heavy ones), and it’s the reason for one famous rule:
Put $null on the left when testing for it: $null -eq $result. If $result happened to be an array, $result -eq $null would filter it instead of answering the question.
1$meals = 120, 45, 340, 0, 80
2$meals -gt 100
3"heavy meals: $(@($meals -gt 100).Count)"
4$meals -contains 0
5$names = 'Ember', 'Glim'
6$names -eq 'glim'
7$null -eq $names120 340 heavy meals: 2 True Glim False
Key takeaways
Operators are words: -eq, -ne, -gt, -lt, -like, -match, -contains, -in; prefix c for case-sensitive.
-like uses wildcards over the whole string; -match uses regex anywhere and fills $Matches.
The left operand decides the type - convert text to numbers before comparing.
Collections on the left filter; put $null on the left when testing for null.
Lesson quiz
7 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write PowerShell scripts
Write a script in the editor and run it for real against sample input, which is piped into your script as $input. Scripts run on PowerShell 6.2 through Try It Online (tio.run), a free public service, so these exercises avoid PowerShell 7-only syntax; your script and test input are sent there.
Tag checker
Every dragon wears a tag like DRG-0042 Ember: DRG-, exactly four digits, a space, then the name. Each input line is a scanned tag. Print Ember: ok (#42) for a valid tag (the number without leading zeros), or bad tag: <line> otherwise.
Use -match with groups and $Matches.
- Morning scan
- Evening scan
Lessons teach PowerShell 7; your script runs on PowerShell 6.2 via Try It Online (tio.run), a free public service, so stick to syntax that works there. Test input is piped into your script as $input. Your script and test input are sent to that service.
Heavy meals
Each input line is the weight of a meal in kilograms. Print the meals over 100 kg on one line (heavy: 120 340), then count: 2, then empty bowl: True or False depending on whether any meal weighed 0. Use comparison operators on the whole collection - and remember the input lines are text.
- Feeding day
- Light day
Lessons teach PowerShell 7; your script runs on PowerShell 6.2 via Try It Online (tio.run), a free public service, so stick to syntax that works there. Test input is piped into your script as $input. Your script and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…