PowerShell Basics: Comparison Operators

PowerShell Basics_ Comparison Operators

Windows PowerShell’s Comparison Operators

For PowerShell newbies one of the strangest comparison operators is -eq.  While you still need the equals sign ‘=’ for declaring variables, in most other cases you need PowerShell’s -eq.

At first using -ne for ‘not equal’ also seems odd, but once you warm to this theme of dash followed by initial letters, then -gt (greater than) or -lt (less than) will seem a logical continuation.  Consequently, abandon > <, instead employ -gt or -lt instead.

Examples of Comparison Operators in PowerShell

A Classic Example of PowerShell’s -eq in a ‘Where clause’

# PowerShell script to list DLLs under system32 folder
$Dir = Get-Childitem C:\windows\system32 -recurse -EA SilentlyContinue
$List = $Dir | Where-Object {$_.extension -eq ".dll"}
$List | Format-Table Name, CreationTime -auto

Learning Points

Note 1:  About the only tricky aspect of the -eq syntax is whether to put the comparison object in single or double quotes.  The difference is that “Double Quotes” expands any variables; whereas ‘single quotes’ are treated as literals.  In this example either “.dll” or ‘.dll’ would achieve the desired listing.  See more about quotes here.

Example of PowerShell’s -eq and -ne Operators

The point of the following real-life script is to test for internet connectivity.  If there is none, then PowerShell can cure this particular browser problem by restarting the dnscache service.

# PowerShell Script to Test An Internet Connection
$DNS = (Test-Connection www.google.com -quiet)
If($DNS -eq "True") {Write-Host "The internet is available"}
ElseIf($DNS -ne "True") {Restart-Service dnscache}

Learning Points

Note 2: We employ an ‘If’ statement to act upon the output of the Test-Connection cmdlet.
Note 3: Remember that instead of an equal sign (=), PowerShell uses -eq.  One benefit is that it’s easy to use the negative -ne (PowerShell’s not equal).

-Eq Operator with .txt File Extensions

# PowerShell Comparison Operator -eq in Foreach Loop
"File Name " + "`t Size" +"`t Last Accessed"
foreach ($file in Get-Childitem)
{If ($file.extension -eq ".txt")
$file.name + "`t " +$file.length + "`t " +    $file.LastAccessTime

Note 4: For the sake of completeness, there is also -Ceq where ‘c’ means case-sensitive.

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Example of PowerShell’s Greater Than Comparison Operator -gt

In PowerShell, greater than is abbreviated to -gt.  Observe how both -gt and -lt (less than) are preceded by a dash.

$FileSource ="D:\"
$Block = Get-ChildItem -path $Filesource -Recurse
$Block | Where-Object {$_.length -gt 2MB}

Learning Points

Note 5:  The point of the above comparison example is to find large files; to be precise to list files greater than 2MB.

Note 6:  As 2MB is a pure number it needs no quotes.

Note 7:  Be aware that if you want to get this example to work you either need a D: drive, or else change the value of $FileSource.

PowerShell’s Less Than Comparison Operator -lt

Just as -eq has an opposite in -ne, so -gt (greater than) has a mirror image in -lt (less than).  In other scripting languages these would be represented by > and < symbols, however PowerShell uses the dash then an abbreviation of the comparison operator, thus -lt.

Speaking of > and <, they have >= and <= to represent greater than or equal and less than or equal.  In PowerShell such concepts involving equal are represented by -ge and -le, where ‘e’ stands for equal.

Here is an example not of -lt, but -le meaning less than or equal.  What it does is calculate and display the 12 times table.

For ( $i = 12; $i -le 144; $i+=12 ) { $i }

Guy Recommends: Free WMI Monitor for PowerShell (FREE TOOL)Solarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor for PowerShell so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guesswork out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

SolarWinds WMI Monitor Download 100% Free Tool

Similar PowerShell Operators

Research with: About_Comparison_Operators

# For even more information about PowerShell Operators try:
Get-Help about_Comparison_Operators

Here is the Complete List of PowerShell’s Comparison Operators


Normally all these comparison operators are not case sensitive. However, to select on the precise UPPER or Proper case precede the operator name with a “c”.
For example, “-cmatch”

See a review of PowerShell math »

Summary of PowerShell’s Comparison Operators

PowerShell uses the equals sign ‘=’ for declaring variables, but for genuine comparison operations you need -eq.  Also, for not equal, use the -ne operator.  When comparing greater than or less than, it is logical to use -gt and -lt, avoid > or < in these instances.

If you like this page then please share it with your friends

See more Windows PowerShell flow control examples

PowerShell Switch Statement  • PowerShell Real-life Techniques  • Free Permissions Analyzer

Differences between For, ForEach and ForEach-Object  • PowerShell Loops  • PowerShell Home

Conditional Operators   • Do While Loop  • PowerShell If Statement  • PowerShell Brackets

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.