PowerShell Comparison Operators: Complete Reference

PowerShell Comparison Operators: Complete Reference

PowerShell Tips Editor 2 min read
PowerShell Comparison Operators: Complete Reference

Using the wrong PowerShell comparison operators produces silent logic bugs that are notoriously difficult to track down — a filter that matches nothing, a condition that always evaluates true, or a substring check that fails because you used -contains instead of -like. This complete reference covers every comparison operator in PowerShell with accurate examples, the case-sensitive variants, and the most common misuse patterns so you always pick the right operator the first time.

Equality: -eq, -ne

-eq (equal) and -ne (not equal) work on strings, numbers, booleans, and objects. String comparisons are case-insensitive by default:

5 -eq 5        # True
5 -ne 3        # True
"Hello" -eq "hello"   # True  (case-insensitive by default)
"Hello" -ceq "hello"  # False (case-sensitive version)

$status = "Running"
$status -eq "Running"   # True
$status -ne "Stopped"   # True

# With null
$x = $null
$x -eq $null   # True

Size: -gt, -lt, -ge, -le

Greater-than, less-than, greater-than-or-equal, and less-than-or-equal work on numbers and on strings (alphabetical comparison). For dates, compare DateTime objects directly:

10 -gt 5       # True
10 -lt 5       # False
10 -ge 10      # True
5  -le 4       # False

# Works on strings (alphabetical)
"banana" -gt "apple"   # True

# Works on dates
(Get-Date) -gt (Get-Date).AddDays(-1)   # True

# Filter processes using more than 500 MB
Get-Process | Where-Object WorkingSet64 -gt 500MB

String: -like, -notlike, -match, -notmatch

-like uses wildcard patterns (* for any characters, ? for one character). -match uses .NET regular expressions. Both are case-insensitive by default:

"PowerShell" -like "Power*"      # True — wildcard
"PowerShell" -like "*Shell"      # True
"PowerShell" -like "Power?"      # False — ? matches one character only
"PowerShell" -notlike "*.exe"    # True

"Server01" -match "^Server\d+$"  # True — regex
"abc123"   -match "\d+"          # True
"abc"      -match "^\d+$"        # False
"Hello"    -notmatch "\d"        # True (no digits)
True
True
False
True

Collection: -in, -notin, -contains, -notcontains

This is one of the most commonly confused operator pairs. The operand order is reversed between -in and -contains:

$services = @('W3SVC', 'MSSQLSERVER', 'Spooler')

# -in: VALUE -in COLLECTION (value on left)
'Spooler'    -in    $services   # True
'Notepad'    -in    $services   # False
'Notepad'    -notin $services   # True

# -contains: COLLECTION -contains VALUE (collection on left)
$services -contains 'Spooler'     # True
$services -notcontains 'Notepad'  # True

# Filter where a property value is in a list
Get-Service | Where-Object Name -in $services

Critical distinction: -contains tests whether a collection contains a specific element. It does not test if a string contains a substring. For substring checks, use -like "*substring*" or -match "pattern".

Type: -is, -isnot

Test whether an object is an instance of a specific .NET type. Useful for handling mixed-type collections and defensive type checking:

42     -is [int]       # True
42     -is [string]    # False
42     -isnot [string] # True
"hi"   -is [string]    # True

$date = Get-Date
$date -is [datetime]   # True
$date -is [string]     # False

# Handle mixed pipeline input
$items = @(1, "hello", (Get-Date), 3.14)
$items | Where-Object { $_ -is [string] }   # Returns "hello" only

Case-Sensitive Variants

Every comparison operator has a case-sensitive variant prefixed with c. Use these when case matters for correctness:

# Default (case-insensitive)
"PowerShell" -eq "powershell"   # True
"abc" -like "ABC"               # True

# Case-sensitive variants
"PowerShell" -ceq "powershell"   # False
"PowerShell" -ceq "PowerShell"   # True
"abc"        -clike "ABC"        # False
"file.LOG"   -cmatch "\.log$"    # False
"file.log"   -cmatch "\.log$"    # True

# Full set: -ceq, -cne, -cgt, -clt, -cge, -cle, -clike, -cnotlike, -cmatch, -cnotmatch, -cin, -ccontains

Common Errors and Fixes

  • -contains tests if array contains value, not substring check. "HelloWorld" -contains "Hello" returns False because -contains checks collection membership, and a string is not a collection of substrings. For substring: use "HelloWorld" -like "*Hello*" or "HelloWorld" -match "Hello".
  • -like needs wildcards to do partial matching. "PowerShell" -like "Power" returns False because without wildcards, -like requires an exact full match. Use "PowerShell" -like "Power*" for prefix matching or "PowerShell" -like "*Shell*" for contains-style matching.

Related Cmdlets / See Also

Wrapping Up

Know the difference between -like (wildcards) and -match (regex), between -in (value in collection) and -contains (collection has value), and between case-sensitive c-prefixed variants and their default case-insensitive counterparts. With these operators precisely understood, your conditions and filters work correctly the first time.

Send-Item -To