PowerShell Comparison Operators

PowerShell comparison operators allow you to find out if the value of a variable contains a string, is it larger, smaller, or equal to some value, etc. Most programming languages use symbols as comparison operators, like <, >, !=, =, however, in PowerShell, pseudo-commands are used instead of these special characters. For example, -eq (equal), -lt (less then), etc.

The table below contains the main PowerShell comparison operators:

-eq Equal (=)
-ne Not equal (<>)
-ge Greater than or equal (>=)
-gt Greater than (>)
-lt Less than (<)
-le Less than or equal (<=)
-like Wildcard comparison
-notlike Wildcard comparison
-match Regular expression comparison
-notmatch Regular expression comparison
-replace Replace operator
-contains Containment operator
-notcontains Containment operator

When performing the comparison operation, PowerShell returns True or False:

echo ("test" -ne "test1")

powershell comparison operators

By default, all PowerShell comparison operators are not case sensitive. However, if you need to perform case-sensitive comparisons, you must add the “c” prefix to the operator. For example, the –ne operator for case-sensitive comparison should be used as follow –cne.

If you need to explicitly indicate that a case-insensitive check is used, the prefix “i” is used. For example, -ine.

You can combine several comparison operators in one expression using the operators: –and, -or, -xor, -not,!:

{$_.Name -eq "John" -and $_.JobTitle -like “*manager*”}

In the similarity operations -like and –notlike, the symbol “*” is used as a wildcard (filter). For example:

($Object.Name -notlike "*system*")

Let’s look at a few examples of using PowerShell comparison operators.

$a=10

if($a -gt 5)

{

Write-host $a " greater than 5"

}

In this example, the comparison operator is used as a condition. If the condition is True, the command contained inside the If condition is executed. In this example, the screen will display the message: “10 greater than 5”.

READ ALSO  Move FSMO Roles and Upgrade Domain to Windows Server 2016

powershell compare operators

Using comparison operators, you can compare strings with each other. For example:

$Address = “9650 Queensway CARLISLE CA10 5XL”

if($Address -clike "*CARLISLE*")

{

Write-host "Your address matches the pattern!"

}

In this example, we compared the $address variable to the case-sensitive string(-clike).

powershell string comparison operators

Also, you can use a regular expressions:

"London" -match "$ondon^"

"London" -notmatch "$don^"

Comparison operators let you compare the values of PowerShell arrays.

$arr1 = "one", "two", "three";

if($arr1 -contains "three")

{

Write-host "It’s OK!"

}

powershell equals operator

You can compare different types of variables and PowerShell objects.

Check that it’s 8 month now:

(Get-Date).Month -eq 8

Check that Windows has a running process called “notepad” that loads the CPU by more than 10%:

Get-Process | where {$_.Processname -eq "notepad" -and $_.CPU -gt 10}

powershell not equal operator

Cyril Kardashevsky

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.