PowerShell Not Equal Operator

Do you need to learn how to use the not equal operator in PowerShell? I will explain in detail with examples. In this tutorial, I will explain various methods to use the not equal operator in PowerShell with various examples.

You can use the not equal operator to check when two values don’t match—like when a city isn’t “Chicago” or a status isn’t “Active”.

PowerShell Not Equal Operator

The PowerShell not equal operator allows you to compare two values and returns $true when they are different and $false when they match. This will work for both numbers, strings, or more complex objects.

Now, let me help you understand how the -ne operator works.

When you run $value1 -ne $value2, PowerShell compares both items:

  • If the values are not equal: returns True
  • If the values are equal: returns False

Let’s say you want to check the population of two US cities:

$newYorkPopulation = 8804190
$losAngelesPopulation = 3898747

$newYorkPopulation -ne $losAngelesPopulation

This returns True because the populations are different. The -ne operator is perfect for data checks where you want to ensure that certain records stand out or don’t match a criterion.

You can see the exact output in the screenshot below:

PowerShell Not Equal Operator

Check out Filter Unique Objects in PowerShell with Where-Object

Compare Strings in PowerShell with -ne

String comparisons are frequent in real-world scripting—think of states, statuses, or office locations.

Example: Excluding a State

$state = "California"

if ($state -ne "Texas") {
Write-Output "This is not Texas."
}

Here, since “California” is not “Texas”, the script outputs “This is not Texas.” The -ne operator makes such checks readable and reliable in administrative scripts.

By default, PowerShell comparisons are case-insensitive. This is important when working with state codes like “NY” vs. “ny”. If you require case-sensitive comparisons, use -cne instead.

$region = "WA"
if ($region -cne "wa") {
    Write-Output "Region does not match case-sensitively."
}

This returns "Region does not match case-sensitively." because the cases do not match.

Not Equal in Filtering Data: Where-Object

One of the most powerful uses of -ne is with Where-Object for filtering lists or arrays.

Example: Return All Employees Not in a Certain Department

Suppose you have a list of employees and want to exclude anyone in the “HR” department.

$employees = @(
    @{Name="Alex Smith"; Department="IT"},
    @{Name="Jamie Lee"; Department="HR"},
    @{Name="Riley Jones"; Department="Finance"}
)

$nonHR = $employees | Where-Object { $_.Department -ne "HR" }

$nonHR

After running this, $nonHR contains only those who aren’t in HR. I use this daily when building reports or refining searches in user management tasks.

Here is the exact output you can see in the screenshot below:

PowerShell Not Equal Operator examples

Read PowerShell Convert Secure String to Plain Text

Comparing Arrays and Collections

The -ne operator also works with PowerShell arrays, returning elements that do not match the comparison value.

Example: List All States Except New Jersey

$states = @("New York", "California", "New Jersey", "Florida")
$nonNJ = $states | Where-Object { $_ -ne "New Jersey" }
$nonNJ

The result is:

New York
California
Florida

Special Cases: Null and Empty Value Checks

Now, let me show you how to use the -ne to check null or empty values in PowerShell.

Example: Find Non-Empty Usernames

$usernames = @("jsmith", "", $null, "awilliams")
$active = $usernames | Where-Object { $_ -ne $null -and $_ -ne "" }
$active

The result:

jsmith
awilliams

This ensures you only process accounts with actual data.

Not Equal with Objects and Custom Comparisons

If you’re comparing objects, the -ne operator assesses their property values.

Example: Filter Servers Not in “Production”

$servers = @(
    @{Name="NY-DC1"; Role="Production"},
    @{Name="CA-EX1"; Role="Development"},
    @{Name="TX-EX2"; Role="Staging"}
)

$nonProd = $servers | Where-Object { $_.Role -ne "Production" }
$nonProd

Now $nonProd lists only servers outside the production environment—essential for safety checks and audits in American enterprise IT setups.

Check out PowerShell Convert XML to CSV

Best Practices and Common Edge Cases

Table: Key Points of PowerShell’s Not Equal Operator

ScenarioOperator UsedCase Sensitive?Returns
Numbers unequal-neNo$True
Strings (default)-neNo$True/$False
Strings (case-sensitive)-cneYes$True/$False
Null check-ne $null$True/$False
  • Always verify which type (string, int, null) you are comparing.
  • Use -cne for strict case checks, especially with state codes and IDs.
  • Trim whitespace in strings to avoid false negatives.
  • Explicitly cast to string if objects might vary in type.

PowerShell provides several other comparison operators. For quick reference:

OperatorMeaningExample
-eqEqual to$x -eq $y
-gtGreater than$x -gt $y
-ltLess than$x -lt $y
-leLess than or equal$x -le $y
-geGreater or equal$x -ge $y
-neNot equal$x -ne $y

Use the right operator to match your data analysis scenario for accurate, readable code.

In this tutorial, I explained how to use the not equal operator in PowerShell.

You may also like:

Leave a Comment

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.