PowerShell CheatSheet – Comparison operators

To check if a condition is true (or false…) one of the key is to compare values. Here are the most common ones.

# "-eq" (==)
is equal to
<#
.EXAMPLE (try it!)
$i = 1000
$test = $i -eq 20
Write-Host "Is `$i ($i) equal to '20'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkRed;
#>

# "-ne" (!=)
is not equal to
<#
.EXAMPLE (try it!)
$i = 1000
$test = $i -ne 20
Write-Host "Is `$i ($i) not equal to '20'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
#>

# "-lt" (<)
is less than
<#
.EXAMPLE (try it!)
$i = 1000
$test = $i -lt 20
Write-Host "Is `$i ($i) less than '20'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkRed;
#>

# "-gt" (>)
is greater than
<#
.EXAMPLE (try it!)
$i = 1000
$test = $i -gt 20
Write-Host "Is `$i ($i) greater than '20'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
#>

# "-le" (<=)
is less than or equal to
<#
.EXAMPLE (try it!)
$i = 1000
$test = $i -le 20
$test2 = $i -le 1000
Write-Host "Is `$i ($i) less than or equal to '20'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkRed;
Write-Host "Is `$i ($i) less than or equal to '1000'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkGreen;
#>

# "-ge" (>=)
is greater than or equal to
<#
.EXAMPLE (try it!)
$i = 1000
$test = $i -ge 20
$test2 = $i -ge 1000
Write-Host "Is `$i ($i) greater than or equal to '20'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
Write-Host "Is `$i ($i) greater than or equal to '1000'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkGreen;
#>

# "-like"
matches, using the wildcard character (*)
<#
.EXAMPLE (try it!)
$i = "PowerShell"
$test = $i -like "Power*"
$test2 = $i -like "power*"
$test3 = $i -like "*Shell"
$test4 = $i -like "*Shll"
Write-Host "Is `$i ($i) is like to 'Power*'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
Write-Host "Is `$i ($i) is like to 'power*'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkGreen;
Write-Host "Is `$i ($i) is like to '*Shell'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test3" -ForegroundColor DarkGreen;
Write-Host "Is `$i ($i) is like to '*Shll'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test4" -ForegroundColor DarkRed;
#>

# "-notlike"
does not match, using the wildcard character (*)
<#
.EXAMPLE (try it!)
$i = "PowerShell"
$test = $i -notlike "Power*"
$test2 = $i -notlike "power*"
$test3 = $i -notlike "*Shell"
$test4 = $i -notlike "*Shll"
Write-Host "Is `$i ($i) is like to 'Power*'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkRed;
Write-Host "Is `$i ($i) is like to 'power*'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkRed;
Write-Host "Is `$i ($i) is like to '*Shell'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test3" -ForegroundColor DarkRed;
Write-Host "Is `$i ($i) is like to '*Shll'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test4" -ForegroundColor DarkGreen;
#>

# "-match"
matches a string using regular expressions
<#
.EXAMPLE (try it!)
$i = "PowerShell"
$test = $i -match "Power"
$test2 = $i -match "power*"
$test3 = $i -match "\w"
$test4 = $i -match "Shll"
Write-Host "Does `$i ($i) matches 'Power'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
Write-Host "Does `$i ($i) matches 'power*'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkGreen;
Write-Host "Does `$i ($i) matches '\w'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test3" -ForegroundColor DarkGreen;
Write-Host "Does `$i ($i) matches 'Shll'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test4" -ForegroundColor DarkRed;
#>

# "-notmatch"
does not match a string using regular expressions
<#
.EXAMPLE (try it!)
$i = "PowerShell"
$test = $i -notmatch "Power\s+Shell"
$test2 = $i -notmatch "\w+"
$test3 = $i -notmatch "Power"
$test4 = $i -notmatch "Shell"
Write-Host "Does `$i ($i) matches 'Power\s+Shell'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
Write-Host "Does `$i ($i) matches '\w+'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkRed;
Write-Host "Does `$i ($i) matches 'Power'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test3" -ForegroundColor DarkRed;
Write-Host "Does `$i ($i) matches 'Shell'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test4" -ForegroundColor DarkRed;
#>

# "-contains"
containment operator, return 'true' if the tested value appears in a set of reference values
<#
.EXAMPLE (try it!)
$i = "PowerShell Scripts"
$test = $i -contains "PowerShell"
$test2 = $i -contains "PowerShell Scripts"
Write-Host "Does `$i ($i) contain 'PowerShell'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkRed;
Write-Host "Does `$i ($i) contain the exact match 'PowerShell Scripts'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkGreen;
#>

# "-notcontains"
containment operator, return 'true' if the tested value appears in a set of reference values
<#
.EXAMPLE (try it!)
$i = "PowerShell Scripts"
$test = $i -notcontains "PowerShell"
$test2 = $i -notcontains "PowerShell Scripts"
Write-Host "Does `$i ($i) not contain 'PowerShell'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test" -ForegroundColor DarkGreen;
Write-Host "Does `$i ($i) not contain the exact match 'PowerShell Scripts'?" -ForegroundColor DarkYellow -nonewline; Write-Host " $test2" -ForegroundColor DarkRed;
#>

 
Bonus: the logical operators!

# "-and" (&&)
needs to match both conditions to be true

# "-or" (||)
needs to match one condition, the other or both to be true

# "-xor" (||)
needs to match one condition or the other (but not both) to be true

 
Bonus (2): the bitwise operators!

# "-band"
bitwise and

# "-bor"
bitwise or

 
Bonus (3): the case sensitive operators!

# "-clike"
matches (case sensitive), using the wildcard character (*)

# "-ceq"
is equal to (case sensitive)

To be able to make powerfull comparisons with strings and pattern, we’ll oftently need regular expressions. Here are the most common ones:
PowerShell CheatSheet – Regular Expressions

Leave a Reply

Scroll to Top