Additionally, if the compared values are equal, PowerShell Not Equal (NE) operator returns False. In this guide, you will learn all you need to know about the “Not Equal (NE)” comparison operator. Moreover, the guide has a lot o real SysAdmin examples. Let’s start with the syntax of the Not Equal (NE)…

Syntax Of PowerShell Not Equal Operator

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

If you want to compare multiple values, use the “and” or “or” operator. The syntax to compare multiple values is… 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: The difference between this last command and the first one is that “c” is added after the “-“.

How To Use PowerShell Not Equal Operator To Test 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: 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: If you now look at the CPU(s) column, all results with null value 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: 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: And here is the result in PowerShell…

How To Use PowerShell Not Equal Operator To Test $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. 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“. 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: Obviously, 1 is NOT equal to 2, so we expect the result to return True. And here is the result in PowerShell… What about… It returned False because that statement is NOT true.

PowerShell Not Equal Operator Examples

In this section, I will discuss two practical examples of the PowerShell “Not Equal” operator. In the first example, I will show you how to use “ne” operator to test multiple values or conditions. Finally, in the second section, you will learn how to use the PowerShell “Not Equal” operator in an IF statement.

PowerShell Not Equal Operator Multiple Values Example

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. 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 us 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 – AJRouter, ALG, and AppMgmt. To remove these services from the result, I will modify the previous command as shown below: 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 “AppIDSvc” OR all services with status NOT equals “running”, run the command below: Unfortunately, the result is not very useful. The reason it is not very useful is this: even though I wanted to eliminate services with status NOT equals stopped, because I have a second condition linked by “or” – the result included both services with status “Running” and “Stopped”. Anyway, I just wanted to illustrate how to test for multiple conditions with the PowerShell “Not Equal” operator!

How to Use PowerShell NOT Equal With 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: Before I run the script, here is the folder saved in the FolderPath variable (line 1 of the code). The folder is empty. If you wish to try the script, change the following variables to an existing path in your computer: $FolderPath: The folder path where you want to create the new folders$TextFilePath: The full path to the text file with the names of the folders you want to create. Finally, to execute the script, copy it to PowerShell ISE and run it. Once I ran the script, the folders were created in the folder specified in the FolderPath variable. 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. Here is an explanation of each line of the script: That is it! Our updated PowerShell “Not Equal” (ne) operator guide. I hope you found this guide helpful. If you found this article helpful, kindly spare 2 minutes to share your experience with our community members at Itechguides Community Forum. Moreover, if you have any questions regarding this article or if the steps did not fix your problem, please post your question at Itechguides Community Forum. Our team and other community members will come back to you with a fix as soon as possible. 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.

References and Further Reading

PowerShell Not Equal Operator  Applications  Examples - 46PowerShell Not Equal Operator  Applications  Examples - 34PowerShell Not Equal Operator  Applications  Examples - 9PowerShell Not Equal Operator  Applications  Examples - 38PowerShell Not Equal Operator  Applications  Examples - 58PowerShell Not Equal Operator  Applications  Examples - 76PowerShell Not Equal Operator  Applications  Examples - 52PowerShell Not Equal Operator  Applications  Examples - 10PowerShell Not Equal Operator  Applications  Examples - 18PowerShell Not Equal Operator  Applications  Examples - 97PowerShell Not Equal Operator  Applications  Examples - 5PowerShell Not Equal Operator  Applications  Examples - 63PowerShell Not Equal Operator  Applications  Examples - 22PowerShell Not Equal Operator  Applications  Examples - 63PowerShell Not Equal Operator  Applications  Examples - 75PowerShell Not Equal Operator  Applications  Examples - 96PowerShell Not Equal Operator  Applications  Examples - 17PowerShell Not Equal Operator  Applications  Examples - 7PowerShell Not Equal Operator  Applications  Examples - 40