Powershell’s Comparison Operators: What’s -eq, -ne, -ceq and -cne?

Adam Bertram

Adam Bertram

Read more posts by this author.

Are you struggling with understanding PowerShell comparison operators like eq, ne, ceq and cne. If so, read on.

Coming from a software development background the first instance I had a “Huh?” moment was when I first saw PowerShell’s comparison operators eq, ceq, cne, etc. All other languages have comparison operators but not quite like PowerShell. I was used to == and <>.

Not only does the syntax look completely different than your typical programming language they also behave differently when dealing comparing collections of values.

It’s something that really hung me up when learning about PowerShell but now it seems like that ol’ beer adage; it’s an acquired taste.

Let’s go over briefly what a comparison operator is if you’re unfamiliar and dive deeper into the nuances that PowerShell introduces. Before long, you’ll be able to wield operators in your PowerShell scripts with no problem.

As I mentioned, all programming/scripting languages have comparison operators. In their most basic sense, comparison operators are necessary to evaluate how different things compare to other things. They are used to take two objects and see what the difference (if any) is between those two objects.

In PowerShell’s case, operators can also be used to find elements inside collections of values.

For this post, I’m going to go over the equality operators and how PowerShell uses them. These are eq and ne with their case-sensitive counterparts ceq and cne. For a full list you can hop over the the Technet site or just use Get-Help about_comparison_operatorsin your Powershell console.

-EQ and -CEQ

If you ever need to see if an object is equal to another object you have to use the eq (case-insensitive) or ceq (case sensitive) operators. These operators test the value of each entity you’d like to compare against.

When I first started learning PowerShell I’d constantly do something like this

$string = 'Adam'
if ($string = 'Adam') {
    'string is equal to Adam'
}

You’ll soon be smacked enough to not do that. The = sign is an assignment operator NOT a comparison operator. You cannot use the = sign to compare one value against another.

The proper way to do this is to define the value to check on the left side of the expression and use the eq operator. Here’s an example of the eq and ceq operators and how they’re used.

PowerShell's eq vs ceq
PowerShell’s eq vs ceq

Notice how eq returned a boolean True value when comparing our variable against adam but ceq returned a boolean False value? This displays the case sensitivity difference between the two operators.

This is all well and good for scalar (single) values but you can also find all instances of particular values using these operators as well. For example:

Using Eq to search arrays
Using Eq to search arrays

If you’ve got a variable with a collection of values such as an array you can use eq and ceq to find all instances of values inside the collection.

Notice that it found all of the instances of the integer 9 we tried to compare against. I use this handy method all the time.

-NE and -CNE

On the opposite side, you’ve both ne and cne. They are exactly the opposite of eq and ceq but display the exact same behavior only opposite. They don’t test equality, they test inequality.

PowerShells ne vs. cne
PowerShells ne vs. cne

I hope this gives a good explanation of the equality comparison operators. This really got me hung up as a newbie and it never really clicked for me until I was just beat into submission by error messages. I hope this post prevents the same beat down I received.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!