PowerShell Comparison operators
Choose the comparison that matches the task, and distinguish a Boolean test from filtering a collection.
Documentation: Microsoft Learn
Overview
The example asks whether 85 is at least 50. -ge produces True for this scalar comparison. -gt would exclude an exact score of 50, so the boundary is part of the requirement.
= assigns; -eq compares. In a filter, an accidental assignment can change the value being examined or fail on a read-only property. Read the operator before investigating the dataset.
Drag the handle — or focus it and use the arrow keys — to compare the mistake with the fix.
04 · Spot the bug
Find it before you run it
Read the snippet below. Something is wrong — see if you can spot it before revealing the answer.
PS> $threshold = 5
PS> if ($score = $threshold) {
>> "Score meets the threshold"
>> }
# The single = assigns 5 to $score and the if-block
# always runs — use -eq to compare.
With a collection on the left, operators such as -eq can return matching elements instead of one Boolean. For a null guard use $null -eq $value. For membership use the appropriate contains or in operator, and remember that their operand order differs.
String comparisons are normally case-insensitive unless you choose a case-sensitive operator such as -ceq. Numeric-looking text from CSV needs conversion before numeric sorting or comparison; validate the conversion instead of assuming every row is a number.
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
$score = 85
$score -ge 50- Stage 1$score
Positional values or no named parameters
02 · Example
An example to inspect
Preview the example. Sign in for the full lesson and activities.
$score = 85
$score -ge 5003 · Walkthrough
What happens
Test scores of 49, 50 and 51. The boundary test tells you more about the condition than repeating it with 85. Then explain whether the task says “at least 50” or “above 50”.
Worth remembering
Test the boundary and the input type. A condition can be syntactically valid while answering the wrong question.
Common mistakes — 6 rules
Traps to avoid
Using = instead of -eq in a filter
An accidental assignment can change the value being examined — or fail on a read-only property — with no error to warn you.
→ guide-eq on a collection returns matches, not a Boolean
With a collection on the left, -eq hands back the matching elements instead of one True/False answer.
→ guidePutting $null on the right
For a null guard use $null -eq $value. Reversing the order misbehaves when the value turns out to be a collection.
Assuming -eq is case-sensitive
String comparisons are normally case-insensitive. Reach for -ceq when the case actually matters.
→ guideSorting numbers as text
Numeric-looking text from a CSV needs conversion before numeric sorting or comparison — validate the conversion.
→ guideOff-by-one with -gt vs -ge
-gt excludes the exact boundary; -ge includes it. Check whether the requirement says “at least” or “above” before you pick.
→ guideReference
Primary documentation
Try it
Before you run anything — predict the output. With $score = 85, what does $score -ge 50 print? What about $score = 49, $score = 50, $score = 51?
Reveal the answers
49 → False · 50 → True · 51 → True. The boundary value 50 is included because -ge means “greater than or equal to”.
Paste these two lines into your own PowerShell, one at a time, and watch the prompt:
$score = 85
$score -ge 50
Then change 85 to 49 and run the second line again. Illustrative output · values vary by environment.
-ge is the comparison operator for “greater than or equal to”. It answers a yes/no question and returns True or False. The single = is different — it assigns a value instead of asking a question. Mixing them up is the most common beginner trap on this page.
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Compares a number and returns a boolean.
Show reference output
Illustrative output · values vary by environment
True
Practise this
Compare numeric values
Return whether 15 is greater than 10. The result must be Boolean.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.