TExcelTAD
Home › PowerShell › PowerShell Comparison Operators

PowerShell Comparison Operators

Comparison operators compare two values and return True or False. In PowerShell they are written as a dash followed by letters. By default they are case-insensitive when comparing strings, which matches how people usually think about names and file paths.

Equality and ordering

Operator Meaning Example (true)
-eq equal 5 -eq 5
-ne not equal 5 -ne 3
-gt greater than 5 -gt 3
-ge greater than or equal 5 -ge 5
-lt less than 3 -lt 5
-le less than or equal 3 -le 3
powershell · try it yourself
$temperature = 23
$temperature -gt 20
$temperature -le 20
'apple' -eq 'APPLE'
'apple' -ceq 'APPLE'
'apple' -lt 'banana'
True
False
True
False
True

Add a c for a case-sensitive version (-ceq, -cne, -clike, -cmatch…) or an i to make case-insensitivity explicit (-ieq). Strings are ordered alphabetically, so 'apple' -lt 'banana' is true.

Wildcards: -like and -notlike

-like compares a string with a wildcard pattern: * matches any number of characters, ? exactly one, and [a-c] one character from a set or range.

powershell · try it yourself
$files = 'report.txt', 'photo.jpg', 'notes.txt', 'data-2026.csv'
$files -like '*.txt'
'---'
'data-2026.csv' -like 'data-20??.csv'
'photo.jpg' -notlike '*.txt'
'B7' -like '[A-C][0-9]'
report.txt
notes.txt
---
True
True
True

Notice the first line: when the left side is an array, comparison operators act as a filter and return the matching elements instead of True/False.

Regular expressions: -match and -replace

-match tests a string against a regular expression. When it succeeds, the automatic variable $Matches holds what was matched, including any capture groups:

powershell · try it yourself
$line = 'ERROR 2026-02-11 disk full on server web01'

$line -match '\d{4}-\d{2}-\d{2}'
$Matches[0]

if ($line -match 'server (?<host>\w+)') {
    "Host: $($Matches.host)"
}

$line -replace '\d', '#'
'web01 web02' -replace 'web(\d+)', 'app$1'
True
2026-02-11
Host: web01
ERROR ####-##-## disk full on server web##
app01 app02

-replace uses a regex too. In the replacement text, $1 refers to the first capture group; keep it in single quotes so PowerShell doesn't treat $1 as a variable. The Regular Expressions lesson goes much deeper.

Containment: -contains and -in

These check whether a collection contains a value. -contains has the collection on the left, -in has it on the right:

powershell · try it yourself
$admins = 'alice', 'bob', 'carol'
$admins -contains 'BOB'
'dave' -in $admins
'dave' -notin $admins
$admins -contains 'al'
True
False
True
False

-contains checks whole elements, not substrings: 'al' is not in the list. To find text inside a string, use -like '*al*', -match 'al' or the .Contains() method (which is case-sensitive).

Type comparison

-is and -isnot check the type of a value (see the Data Types lesson):

powershell · try it yourself
42 -is [int]
'42' -isnot [int]
(Get-Date -Date '2026-01-01') -is [datetime]
True
True
True

Comparing with $null

Put $null on the left when testing for null. If the variable on the left is an array, -eq filters it instead of answering the question:

powershell · try it yourself
$values = @(1, $null, 3)
$values -eq $null          # a filter: returns @($null), which prints as nothing
($values -eq $null).Count
$null -eq $values
1
False

$values -eq $null returns the null elements of the array, while $null -eq $values correctly says the array itself is not null. PSScriptAnalyzer warns about $x -eq $null for exactly this reason.

Test yourself

What does 'Hello' -eq 'hello' return?

  1. True
  2. False
  3. An error
  4. $null

What does 'a.log', 'b.txt', 'c.log' -like '*.log' return?

  1. True
  2. False
  3. a.log and c.log
  4. 2

Which expression is true?

  1. 'apple pie' -contains 'apple'
  2. 'apple' -in 'apple', 'pear'
  3. 'pear' -notin 'apple', 'pear'
  4. @('a', 'b') -contains 'ab'