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 $losAngelesPopulationThis 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:

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" }
$nonHRAfter 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:

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" }
$nonNJThe result is:
New York
California
FloridaSpecial 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 "" }
$activeThe result:
jsmith
awilliamsThis 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" }
$nonProdNow $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
| Scenario | Operator Used | Case Sensitive? | Returns |
|---|---|---|---|
| Numbers unequal | -ne | No | $True |
| Strings (default) | -ne | No | $True/$False |
| Strings (case-sensitive) | -cne | Yes | $True/$False |
| Null check | -ne $null | – | $True/$False |
- Always verify which type (string, int, null) you are comparing.
- Use
-cnefor 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.
Related Comparison Operators in PowerShell
PowerShell provides several other comparison operators. For quick reference:
| Operator | Meaning | Example |
|---|---|---|
| -eq | Equal to | $x -eq $y |
| -gt | Greater than | $x -gt $y |
| -lt | Less than | $x -lt $y |
| -le | Less than or equal | $x -le $y |
| -ge | Greater or equal | $x -ge $y |
| -ne | Not 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:
- PowerShell Match Operator Examples
- PowerShell Filter Operators
- PowerShell Arithmetic Operators
- PowerShell Logical Operators
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.