How to Use PowerShell Not Equal (-ne) Operator: Examples and Best Practices

PowerShell Not Equal is a powerful command that allows developers, IT professionals, and DevOps to manage system administrative tasks by comparing values in various ways. The syntax for the Not Equal operator is the “-ne”, and it can compare strings, integers, and other data, using the Not Equal operator.
At ServerMania, we take the next step and go beyond hosting by helping businesses and organizations adopt the full power of automation and scripting tools like PowerShell. We offer SQL Server Hosting Solutions, dedicated server hosting, and cloud hosting to provide infrastructure tailored to help you manage critical workloads with ease.
In this quick tutorial, we’ll show you how to use the Not Equal operator, learn the best practices, and go over many examples that you can confidently deploy immediately.
What is PowerShell?
PowerShell is a command-line and also an entire scripting language crafted specifically for Windows PowerShell and integrates well with the Windows operating system. In contrast to other traditional shells, PowerShell is deeply compatible with the .NET Framework, allowing administrators to execute commands, write advanced scripts, and craft functions to automate repetitive administrative tasks.
PowerShell is also a tool that allows IT specialists or even system users to manage local configurations and remote servers by providing schedules, registry tweaks, service management, and file verification.
The scripting language delivers a wide variety of expressions and operators, making the environment easy to navigate and intuitive. You can easily inspect string values, compare values, evaluate boolean values, or leverage the if statement to outline a logic.
PowerShell divides its operators into eight groups:
- Arithmetic operators (perform calculations on numbers and integers)
- Assignment operators (used to assign data to a variable)
- Comparison operators (such as -eq, -ne, and others for evaluating two values)
- Logical operators (to handle complex statements and boolean logic)
- Redirection operators (to control output and write to files)
- Split and join operators (for handling arrays and string values)
- Type operators (for checking objects and two objects compatibility)
- Unary operators (which work with only one expression)
These operators can be combined with other features such as parameters, aliases, comment-based help, and script blocks to deliver a quick and easy way to build powerful scripts. This combination of functionality and accessibility makes PowerShell ideal for administrators who want to learn PowerShell and verify security across their environmental infrastructure.
See Also: How Much Does a Windows Server Cost to License? Editions Pricing Guide
PowerShell Arithmetic Operators: Quick Overview
In addition to comparison operators, PowerShell provides a set of arithmetic operators that allow you to perform calculations on numbers and integers.
These operators are fundamental for scripting, automating tasks, and creating dynamic expressions. They can be combined with assignment operators, logical operators, and unary operators to build more complex logic in your scripts.
The most commonly used arithmetic operators in PowerShell include:
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 → Returns 8 |
– | Subtraction | 10 – 4 → Returns 6 |
* | Multiplication | 6 * 7 → Returns 42 |
/ | Division | 20 / 5 → Returns 4 |
% | Modulus (remainder) | 10 % 3 → Returns 1 |
Example Commands:
$a = 10
$b = 3
# Addition
$a + $b # Returns 13
# Subtraction
$a - $b # Returns 7
# Multiplication
$a * $b # Returns 30
# Division
$a / $b # Returns 3.3333
# Modulus
$a % $b # Returns 1
These arithmetic operators can also be combined with assignment operators like += or -= to create variables that automatically update their values. For instance:
$count = 5
$count += 2 # Now $count = 7
See Also: What Are The Server Requirements for Windows Server 2016
Windows PowerShell – Basics of Not Equal Comparison Operators
The “-ne” operator in PowerShell checks and determines whether two values are not equal and delivers a Boolean value. If the response is True, this means that the values are not the same; therefore, False signals that they are the same.
That’s it, so let’s go over some essentials:
Comparing Variables
The comparison of two variables is one of the most popular uses, where PowerShell evaluates whether two values are not equal. For example, let’s try to determine whether two variables, $a and $b, are equal, by using the syntax below:
$a -ne $b
This comparison will return Results if $a and $b do not have equal values. If not, it will return False.

Follow the steps below to compare values using the PowerShell Not Equal operator:
Step 1: Constructing Variable
The first step we need to undertake is to construct a variable for this example. In order to achieve this, we must use the PowerShell console to execute a command.
The variables $a, $b, $c, and $d each represent a different value, so to determine whether these values are equal, you have to compare each one of them. To demonstrate how the Not Equal operator works with equal values, you set $c and $d to the same value.
$a = 1
$b = 2
$c = 3
$d = 3
Step 2: Not Equal Comparison
To find out whether the values of $a and $b are equal or not, use the PowerShell Not Equal comparison:
$a -ne $b
Since $a has a value of 1 and $b of 2, which are not equal, the result is True.

Given that the values of $c and $d are equal (3=3), the console will return True output, as displayed in the image above.
Step 3: Comparing Variables
Also, compare variables $c and $d to determine if they are not equal.
$c -ne $d
Given that the values of $c and $d are equal (3=3), you will observe a False output return.

Comparing Values
You can compare multiple values directly with the PowerShell Not Equal operator when objects are not in variables. For instance, the command below uses the PowerShell Not Equal operator to compare two values explicitly.
5 -ne 3
Because the values aren’t equal, a True value will be returned, as illustrated below.

Removing a Value from the Array
When two values are not equal, the PowerShell Not Equal operator gives a Boolean result. In addition to that feature, this operator can exclude certain elements from an array.
For instance, construct a variable called $numbers that holds an array of the digits 1 through 5.
# Create an array containing numbers 1 through 5.
$numbers = 1..5
# Display the array
$numbers

Run the following command after that to return the array with the exception of one number (4)
$numbers -ne 4
You can see that, except for the number 4, the command returned all of the numbers in the array.

Comparing String Variables
Aside from using the PowerShell Not Equal operator to compare numbers, you can also use it to compare strings. Follow the steps below:
Step 1: Comparing Two Strings
Run the command below to see if two strings, “Hello” and “World,” are not equal.
"Hello" -ne "World"
Since “Hello” and “World” are not equal, the result is True.

Step 2: Case Sensitive Comparison
Compare to determine if the strings “Hello” and “hello” are equal.
"Hello" -ne "hello"
The outcome is False since “Hello” is equal to “hello”, because “-ne” is not case sensitive.

Step 3: Using the -cne Operator
There is no case sensitivity with the PowerShell Not Equal operator. The preceding step showed that “Hello” is equal to “hello”. But instead of using the -ne operator, use the -cne operator, which stands for Case Sensitive Not Equal, to compare string values if case sensitivity is necessary.
"Hello" -cne "hello"
Therefore, PowerShell no longer considers “Hello” and “hello” to be equal.

Filtering Files and Directories
Whenever working with many folders, filled with different files, a very popular and common PowerShell application is the (-ne) operator in combination with comparison operators. This provides the capability of filtering files within a folder. For example, you might want to create a list of files that exist in a folder but exclude a certain type, such as .log files.
Use the following expression to filter .log files:
# List all files in C:\Users\Public\Documents except .log files
Get-ChildItem -Path "C:\Users\Public\Documents" | Where-Object { $_.Extension -ne ".log" }

The syntax uses Where-Object and a logical operator to evaluate an expression inside parentheses. If the condition evaluates to not equal, the filter will return True, and the item will be displayed. You can also combine multiple operators to apply more detailed logic. For instance, exclude both .log and .tmp:
Get-ChildItem -Path "C:\Users\Public\Documents" | Where-Object { ($_.Extension -ne ".log") -and ($_.Extension -ne ".tmp") }

Notice the use of parentheses to make the code easier to read, which is a best practice when mixing comparison operators and logical operators.
Another real-world case is skipping a specific file name:
# Exclude readme.txt
Get-ChildItem -Path "C:\Users\Public\Documents" | Where-Object { $_.Name -ne "readme.txt" }

This style of filtering can be combined with functions, assignment operators, or even used inside an if statement when building automation scripts. For example, you could write a short script that loops through files and throws an exception if a required file does not exist, or you could use Test-Path as a quick check.
Compared to other languages, PowerShell’s approach is concise, especially when using aliases (e.g., gci for Get-ChildItem) and adding inline comment-based help.
This flexibility makes it easier to create reusable scripts for backups, cleanups, or system checks while maintaining clear syntax and reliable filtering.
PowerShell Not Equal Operator | Real World Examples
This section puts the PowerShell Not Equal operator into use in the real world. However, let’s consider the following:
Monitoring Services Status
Monitoring server service status is a fundamental aspect of Windows systems administration. In order to complete this process, the services whose Status Is Not Running must be filtered out.
For instance, use the Get-Service cmdlet to see a list of every service installed on your machine.
Get-Service

The output listed above shows both operating and halted services.
Run the command below and use the -ne operator to filter the services that are not running:
Get-Service | Where-Object {$_.Status -ne "Running"}

After using the PowerShell Not Equal comparison, as seen below, the Status column only displays the services that have been halted.
In the same way, use the command below to retrieve the services whose Status is Not Stopped:
Get-Service | Where-Object {$_.Status -ne "Stopped"}

However, only services that are currently active are visible.
Combining PowerShell Not Equal with the AND Operator
The PowerShell -and operator allows you to pair the Not Equal condition with another comparison operator, such as -eq (equal). This approach is helpful if the object you’re comparing needs to satisfy several requirements.
A standard requirement for monitoring server services, for instance, is that all Automatic services be active. Run the command below to get a list of all services with the Status Not Running and StartType Automatic flags.
In this case, the object value has to satisfy every requirement.
Get-Service |
Where-Object {$_.Status -ne "Running" -and $_.StartType -eq "Automatic"} |
Select-Object Status,StartType,Name

You can now identify the automatic services that are not active, which can guide your decision-making.
If/Else Conditional Logic
You can use the PowerShell Not Equal operator in the if/else statements and other conditional expressions. A service, for instance, must normally be active if its StartType value is Automatic.
The following script receives a list of all services, determines which automatic services are not running, and then performs the necessary restorative steps to start the service and display a notice.
Start-Service $_. Name is commented out and won’t execute. To run the command, remove the comment from this line.
Use PowerShell to execute the code below.
Get-Service -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.Status -ne 'Running' -and $_.StartType -eq 'Automatic') {
"The service [$($_.Name)] StartType is [$($_.StartType)] but its current Status is [$($_.Status)]"
# Start-Service $_.Name
}
}
![A PowerShell Command Prompt showing the statement "Get-Service -ErrorAction SilentlyContinue | ForEach-Object { if ($_.Status -ne 'Running' -and $_.StartType -eq 'Automatic') { "The service [$($_.Name)] StartType is [$($_.StartType)] but its current Status is [$($_.Status)]" # Start-Service $_.Name } }".](https://images.surferseo.art/558a5714-9409-46a4-8cd3-1add74e99424.png)
Your result should display any automatic services that are not active.
See Also: How to Install Windows Server 2019
Mastering Null Comparisons
In PowerShell, proper handling of $null is crucial to avoid common scripting errors. One key rule is always placing $null on the left side of a comparison. This prevents issues when testing collections, which many scripts fail to handle correctly.
Correct Example:
if ($null -ne $myArray) {
# Safe to process array
}
Incorrect Example:
if ($myArray -ne $null) {
# May not work as expected with collections
}
Common pitfalls include confusing empty, null, and zero values, leading to unexpected results. Best practices for defensive coding include consistently using $null on the left, adding explicit checks, and combining logical operators for complex conditions.
Learning PowerShell – Wrapping Up
As shown in this quick tutorial, PowerShell Not Equal is a tool and scripting language that can be used to automate tasks, create scripts, and manage systems and networks. With the wide variety of commands, PowerShell can assist you in simplifying IT tasks and automating repetitive workloads quickly and easily.
Learning how to use PowerShell is a worthwhile investment of time and effort, but with a bit of practice, you can easily become a PowerShell expert and use PowerShell to its fullest potential.
Find more detailed information in our in-depth PowerShell Automation Guide.
Was this page helpful?