Comparison Operators in PowerShell

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

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

You may also like:

Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...
How to transfer FSMO Roles From a Failed Domain Co... In case domain controller, which owns FSMO (Flexible Single Master Operation) roles, is fail (virus attack, fatal software problems or catastrophic ha...
Time Configuration for a Virtualized Domain Contro... Today we will talk about some of the features of time configuration on a virtualized domain controllers. Typically, the time synchronization scheme in...
Get-service: Checking the Status of Windows Servic... Using the Get-Service cmdlet you can get a list of all the services installed on the operating system, its status and startup type. This one and other...
Installing WebDAV client on Windows Server 2016 WebDAV (Web Distributed Authoring and Versioning) is an extension of the HTTP/1.1 protocol. The WebDAV protocol allows to access files on the remote s...

Add Your Comment