Ezine 206 – PowerShell’s Comparison Operators

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.

Topics for Comparison Operators in PowerShell

 ♣

This Week’s Secret

My problem is not with -eq (equals) but with -ne, not equal.  I have an overwhelming urge to use -neq instead of plain -ne.  Once I overcome this foible employing PowerShell’s comparison family of operators is straight forward.

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

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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

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

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

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

 


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

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.