PowerShell Genie

PowerShell Comparison operators

Choose the comparison that matches the task, and distinguish a Boolean test from filtering a collection.

Lesson 2 min read Windows PowerShell 5.1 and PowerShell 7

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.

Before
PS> Get-Service | Where-Object { $_.Status = "Running" }

# Assigns "Running" to every service instead of
# comparing — wrong results, and no error to warn you.
After
PS> Get-Service | Where-Object { $_.Status -eq "Running" }

# Compares each service status — returns only the
# services that are actually running.

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
  1. Stage 1$scorePositional 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 50

    03 · 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

    Mistake

    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
    Gotcha

    -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.

    → guide
    Mistake

    Putting $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.

    → guide
    Gotcha

    Assuming -eq is case-sensitive

    String comparisons are normally case-insensitive. Reach for -ceq when the case actually matters.

    → guide
    Mistake

    Sorting numbers as text

    Numeric-looking text from a CSV needs conversion before numeric sorting or comparison — validate the conversion.

    → guide
    Gotcha

    Off-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.

    → guide

    Reference

    Primary documentation

    Microsoft Learn: comparison operators

    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”.

    Illustrative output · values vary by environment

    1PS> $score = 85
    2PS> $score -ge 50

    Example reader

    Inspect this page's example

    Fictional browser data when supported

    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.

      Review the lesson

      Keep going

      Next: PowerShell Regex and wildcards
      Genie guidePowerShell Comparison operators

      Choose the comparison that matches the task, and distinguish a Boolean test from filtering a collection.