PowerShell Not Equal Operator: Applications, Examples

Photo of author

By Victor Ashiedu

Published

Do you want to test for negative equality in your PowerShell script? You need the Not Eqla (NE) operator and this guide teaches you all you need to know about it.

Overview

PowerShell Not Equal operator compares two values. It returns True if the two values are not equal.

Additionally, if the compared values are equal, PowerShell Not Equal (NE) operator returns False.

Syntax of the Not Equal Operator

The syntax of “Not Equal (NE)” operator is..

"item 1" -ne "item 2"

If you want to compare multiple values, use the “and” or “or” operator. The syntax to compare multiple values is…

("item 1" -ne "item 2") -or ("item 3" -ne "item 4")
("item 1" -ne "item 2") -and ("item 3" -ne "item 4")

The first syntax of the PowerShell Not Equal operator retunes True if item 1 is NOT equal to item 2. Moreover, in the second syntax, the command will return True if either of the “Not Equal” comparisons on both sides of the “Or” operator is True.

Unlike the second syntax with the “or” operator, the third syntax – with the “and” operator – will only return True if the “Not Equal” comparisons on both sides of the “and” operator are True.

Finally, the “ne” operator is not case sensitive (it is case-insensitive). However, if you want to make it case sensitive, use the syntax below:

"item 1" -cne "item 2"

The difference between this last command and the first one is that “c” is added after the “-“.

Example 1: Test for Null or Empty Values

In PowerShell scripting, there are so many reasons you may want to test for null or empty values. You can use the “ne” to test if a value is null.

To test for the null value, use the $null automatic variable. Moreover, you can also use the PowerShell “Not Equal” operator test for an empty value in PowerShell with “” (2 double-quotes without anything within the quotes).

Let’s start with some simple examples…

Open a PowerShell prompt and run the command below:

Get-Process | Select-Object -First 10

The reduce the results, I piped Get-Process to Select-Object and retired the first 10 processes.

The command returns all processes running on your local computer. I have highlighted the CPU(s) column.

For some processes, the CPU(s) column is blank (null, empty).

To display only results where the CPU(s) column is NOT null or blank, run the command below:

Get-Process | Select-Object -First 10  | Where-Object {$_.CPU -ne $null}

If you now look at the CPU(s) column, all results with null values have been eliminated!

You can also apply the same principle to return all services on your computer with status “running”. Here is the PowerShell command:

Get-Service | Where-Object -FilterScript {$_.status -ne "Stopped"}

Here is the result of the command in PowerShell:

To make the result look better, we can pipe the output into Format-Table – then, re-arrange the headers. Here is the modified command:

Get-Service | Where-Object -FilterScript {$_.status -ne "Stopped"} | Format-Table Name, DisplayName, Status

And here is the result in PowerShell…

Example 2: Test for $True or $False

It may not be obvious however, every time you use PowerShell “Not Equal” operator in a command, it returns a $True or $False. The $True, or $False value is then used to execute any further commands.

$True, $False are automatic variables that mean True and False respectively.

As an example, in the Get-Process example in the last section, the “$_.CPU -ne $null” statement within Where-Object simply returned all processes that returned True for the statement, “$_.CPU -ne $null“.

Get-Process | Where-Object {$_.CPU -ne $null}

Furthermore, all processes that returned False for the, “$_.CPU -ne $null” statement were not returned.

To drive my point home, run the command below in PowerShell:

1 -ne 2

Obviously, 1 is NOT equal to 2, so we expect the result to return True. And here is the result in PowerShell…

What about…

1 -ne 1

It returned False because that statement is NOT true.

Example 3: Test with Multiple Conditions

To illustrate how to use the PowerShell “ne” operator to test multiple values, let’s return to the Get-Service command example in the previous section.

Get-Service | Select-Object -First 20 | Where-Object -FilterScript {$_.status -ne "Stopped"}

In this command, we used the PowerShell “Not Equal” operator to test for a single condition. The command returned only services that DO NOT have their status as “Stopped”.

The condition is tested within the Where-Object part of the command. Here is the result of the command:

As expected, the command returned ALL services with their status as “Running”.

What if I want to eliminate some services even though their status is “Running”?

I can achieve this by adding multiple values (or conditions) in the Where-Object part of the command. For example, let’s say I do not want to return the following services – ACCSvc, Audiosrv, and BFE.

To remove these services from the result, I will modify the previous command as shown below:

Get-Service | Select-Object -First 20 | Where-Object -FilterScript {$_.status -ne "Stopped" -and ($_.Name -ne "ACCSvc") -and ($_.Name -ne "Audiosrv") -and ($_.Name -ne "BFE") }

The result of the modified command does not include these 3 services!

Alternatively, you can alter the result of the last command by using “or” in place of “and”.

For example, to return all servers not named “AppXSvc” OR all services with status NOT equals “running”, run the command below:

Get-Service | Select-Object -First 20 | Where-Object -FilterScript {($_.status -ne "running") -or ($_.name -ne "AppXSvc") }

Unfortunately, the result is not very useful. The reason it is not very useful is this: even though I wanted to eliminate services with the status NOT equals stopped because I have a second condition linked by “or” – the result included both services with the status “Running” and “Stopped”.

Anyway, I just wanted to illustrate how to test for multiple conditions with the PowerShell “Not Equal” operator!

Example 4: Use Not Equal in an IF Statement

In this example, I will build a script that creates folders using the names in the text file below.

Here is the script that creates folders using names shown in the text file shown above:

$FolderPath = "D:\PS-Tutorial\NewFolders" 
$TextFilePath = "D:\PS-Tutorial\word_Occurrences.txt"
$folderNames = Get-Content $TextFilePath
 ForEach ($folderName in $folderNames) {
 $checkPath = Test-Path $FolderPath\$folderName
     If ($checkPath -ne $true) {
  New-Item -Path $FolderPath -Name $folderName -ItemType "directory" | Out-Null }
 }

When I executed the script, it created the folders with the names of the string in the text file.

Even though I had some of the strings in the text file multiple times, the script created 1 folder each because it overwrote the folders.

Below I have explained what each line of the script does. Pay attention to the PowerShell “Not Equal” operator path – “$checkPath -ne $true” part – line 6.

$FolderPath = "D:\PS-Tutorial\NewFolders" 
$TextFilePath = "D:\PS-Tutorial\word_Occurrences.txt"
$folderNames = Get-Content $TextFilePath
 ForEach ($folderName in $folderNames) {
 $checkPath = Test-Path $FolderPath\$folderName
     If ($checkPath -ne $true) {
  New-Item -Path $FolderPath -Name $folderName -ItemType "directory" | Out-Null }
 }

Here is an explanation of each line of the script:

  1. Line 1: Saves the path you want to save the new folders in a variable called FolderPath.
$FolderPath = "D:\PS-Tutorial\NewFolders" 
  1. Line 2: Saves the path to the text file with the folder names, in a variable called TextFilePath.
$TextFilePath = "D:\PS-Tutorial\word_Occurrences.txt"
  1. Line 3: Extracts the content of the text file (the folder names), with the Get-Content command. Then, save the result in the folderNames variable.
$folderNames = Get-Content $TextFilePath
  1. Line 4: Opens the ForEach statement block. The ForEach statement iterates the result of the text file (saved in the folderNames variable). Then, at each iteration, it saves the name of the folder in a temporal variable, folderName.
ForEach ($folderName in $folderNames) {
  1. Line 5: Executes within the ForEach statement block: Uses the Test-Path command to check if the path to the folder exists in the folder path saved in the FolderPath variable.

    The result of the Test-Path command is $True or $False.
 $checkPath = Test-Path $FolderPath\$folderName
  1. Line 6: Introduces the IF statement (still within the ForEach statement block). This is where we use the PowerShell “Not Equal” operator to check if the result of the Test-Path command is $True or $False.
   If ($checkPath -ne $true) {
  1. Line 7: In this line, if the “$checkPath -ne $true” is NOT equals True, use the New-Item command to create a folder with the name saved in the temporal variable, folderName.

    Additionally, to stop the New-Item command from displaying any results, pipe it to the Out-Null command.

    Finally, close the IF statement block with a close bracket “}”.
  New-Item -Path $FolderPath -Name $folderName -ItemType "directory" | Out-Null }
If you run the New-Item command without piping it to the Out-Null command, it displays the details of the folder created.
  1. Line 8: Close the ForEach statement block with a close bracket “}”.
}

Conclusion

The NE conditional operator is a very essential PowerShell scripting tool. I have explained – with examples – various ways you can use this operator.

To help us continue to improve our content, we like to hear your feedback. The fastest way to let us know what you think about this article is by responding to the “Was this page helpful?” question below.

Alternatively, you may share your thoughts with the community – or even ask a question – using the “Leave a Reply” form located at the bottom of this page.

Finally, for more PowerShell tech Itechguides, visit our Windows PowerShell How-To guide page. You may also find our Work from Home page very helpful.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment