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 > < 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: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Network Performance Monitor
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 now.
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 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
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft's 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. Give this WMI monitor a
try - it's free.
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"
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 <.
If you like this page then please share it with your friends
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
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.