If you work with PowerShell, sooner or later you will require PowerShell operators in your commands and scripts. This post explains comparison operators and logical operators in PowerShell 3.0.
Latest posts by Timothy Warner (see all)

In computer programming, operators are symbols that are used to create expressions. Going further, an expression is an instruction that uses operators and data in order to produce a result. We can use conditional and logical operators in Windows PowerShell to (among other things) filter the result of an expression. Specifically, this translates into reducing the output of a PowerShell cmdlet to a more manageable amount.

Comparison Operators

For instance, consider the following expression:


As you know, the Get-Process cmdlet is used to retrieve a list of processes that are running on a local or remote computer. However, returning a list of all processes during a troubleshooting operation makes it much more difficult to see the specific information we need.

Thus, we can combine (a) the PowerShell pipeline; (b) the Where-Object cmdlet; and (c) comparison operators to filter cmdlet results in much the same way that the WHERE clause filters rows in Structured Query Language (SQL). To wit:

Get-Process | Where-Object {$_.name –eq “svchost”}

Example with the PowerShell operator -eq

Example with the PowerShell operator -eq

Let’s break down each component of the preceding syntax:

  • | :The pipe character feeds the output of one cmdlet as an input argument in another cmdlet
  • $_ : This special syntax denotes “this object.” In other words, we want to access a property or method of each individual item of the current object (in this example, the “current object” is the output of Get-Process)
  • .name: This is an example of a property, which is a descriptive or functional attribute of an object. The name property refers to the process name
  • -eq: This is the “kicker” that confuses many Windows admins who don’t (yet) fully understand PowerShell. To denote equality in PowerShell, we use –eq instead of = or ==.
  • {}: The Where-Object cmdlet requires that its own expression be enclosed in braces

So today we learned that the equality comparison operator in Windows PowerShell is –eq. What are the other comparison operators? Let me help you out:

  • -lt : less than
  • -le : less than or equal to
  • -gt : greater than
  • -ge : greater than or equal to
  • -ne : not equal to
  • -like : pattern matching

How can we use comparison operators and the Where-Object cmdlet to see only those processes with a CPU utilization of over 20 percent? Try this:

Get-Process | Where-Object {$_.cpu –gt 20}

Example with the PowerShell operator -gt

Example with the PowerShell operator -gt

By the way, you can get a list of all properties and methods (actions that an object can perform) of a cmdlet’s output by using the following syntax:

$gp = Get-Process
$gp | Get-Member

Variables in Windows PowerShell start with a dollar sign ($). First we need to pack the results of Get-Process into a variable. We can then pipe the contents of the variable into the Get-Members cmdlet. I show you the results of these statements on my computer in Figure 1.

Logical Operators

We use logical operators to evaluate an expression against the Boolean values TRUE and FALSE. What’s cool about logical operators such as AND and OR are that we can use a single expression to test for multiple true/false conditions. Consider the following example:

Get-Process | Where-Object {$_.handles –le 150 –and $_name –like “s*”}

In the previous code, we want to see all processes who have handle counts of less than or equal to 200, and a name that begins with “S.” Here is the full list of logical operators that are supported by PowerShell

  • -and : True only when both statements are true
  • -or : True when either or both statements are true
  • -xor : True only when one of the statements is true and the other is false
  • -not : Negates the statement that follows it

Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account