Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



 PowerShell's 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.  Also, for not equal, use the -ne operator.  Once you warm to this theme of dash followed by initial letter, then -gt (greater than) or -lt (less than) will seem a logical continuation.  Consequently, abandon > < and 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
$List = $Dir | where {$_.extension -eq ".dll"}
$List | Format-Table Name, CreationTime -auto

Learning Points

Note 1:  About the only thing that could go wrong with 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 "Internet Available"}
Elseif($DNS -ne "True") {Restart-Service dnscache}

Learning Points

Note 2: We employ an 'if' statement to act upon the output of test-connection.

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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

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.

Clear-Host
$FileSource ="D:\harvest\LotsFiles.txt"
$Block = Get-Content -path $Filesource
$Block | Select-Object | Where-Object {$_.length -gt 254}

Learning Points

Note 5:  The point of the above comparison example is to find long files; to be precise to list filenames greater than 254 characters.

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

Note 7:  Be aware that if you want to get this example to work you would need a file containing the list of filenames that you wish to check.  Also be prepared to 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 }

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 and not > or <.

Similar PowerShell Comparison Operators

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

 


See more PowerShell examples for syntax advice

PowerShell Tutorials  • Syntax  • PowerShell functions  • Plist

Get-Date  • Quotes  • PowerShell variables  • RegEx 

Conditional operators  • Hashtable   • Windows PowerShell

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.