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