Powershell

PowerShell Not Equal

There are many comparison operators you must have heard about. One of them is the “Not Equal” operator. It returns “False” if both values are the same and “True” if values are different. The “Not Equal” operator can work on characters, integers, and string values equally. Within this guide, we will see the implication of the “Not Equal” operator on all types of values within the PowerShell of the Ubuntu 20.04 Linux system. Let’s begin by opening the shell terminal first with Ctrl+Alt+T. Within the terminal, open the PowerShell application using the simple “pwsh” command as below.

Example 01: Compare Strings

Now the Powershell has been started, let’s begin with the Equal operator first. So, we have to use the “Equal” operator as “eq” within two string values with a slight change. The “eq” has been headed by a minus sign. The first string has capital “L” in it while the other has all the lowercase values, although both the strings are the same. Upon executing this line of code, we got “True” as the “Equal” operator works just fine without considering case sensitivity.

Now, check out the “Not Equal” operator. It will be used as “h-ne” within the code line. So, we are using the same string values to be compared in the PowerShell while the “-ne” operator is in between them. The first line of code shows two strings, with one having capital letters and the other with no capital letters. It returns “False” as both the values are equal. On the next line of code, we have used both the strings with the same case. It also returns “False” because both the values are equal, and the condition says that these are not equal, which is not true.

Example 02: Compare Characters

Let’s take a look at using the “Not Equal” operator on some characters as well. So, we have used the “-ne” operator within capital character “A”, and small character “a”. The first line of code returns False, as both the characters are equal logically, i.e., same alphabets. While the next line of code returns “True” as a result because both the alphabets “A” and “B” are not the same. This is how the Not equal operator works on characters or single alphabets.

If you want to check the case sensitivity of characters or strings, you have to use the “case” flag as “-cne” within the Not Equal operator. Let’s take a look at it once. At the first line of code, we have been checking the same alphabets with different cases. It returns True because due to case sensitivity, both are not equal. On the next line of code, we have used the same case for the same alphabets. As a result, we got False.

Example 03: Compare Integers

The “Not Equal” operator works the same on the integers as it works on strings. So, we have used two integers to be compared with the “-ne” operator. The first line returns “True” as both integers are different, while the other returns “False” as both are the same.

Example 04: Comparison of Two Conditions

Let’s use the “Not Equal” operator within more than two statements to check the equality. We will be using the “OR” operator to give out the final Boolean result. So, we have been using it within the two conditions. Thus, both the statements before and after the “OR” operator compare the same words while using the “Not Equal” operator within. The “OR” operator displays “False” if both the conditions before and after it turns out “False”. Hence, we got “False” in return as both conditions don’t meet.

Then we have used the “Not Equal” operator to compare equality of quite different values within both the conditions before and after the “OR” operator. The “OR” operator returns True if both the conditions meet. So, within this scenario, both conditions return True. Therefore, we got “True” in return.

If one of the conditions is “True”, the OR returns “True”. So, we got “True” in return below.

The concept of checking more than two conditions can also be applied to the integers. So, we have used some lines of code to compare integers with the “Not Equal” operator. The “OR” operator has been used as before. You can see that the use of “OR” returns “False” if both the conditions before and after it turns out to be “False”. Otherwise, if a single condition turns out to be “True”, it returns “true”.

Example 05: Compare Variables

You can also apply the “Not Equal” operator on variables to check if the two specified variables are equal or not. Thus, we have stated two string type variables separately, i.e., “a”, and “b”. A new variable has been checking if both the variables “a”, and “b” are not equal using the “Not equal” operator within them. As both the variables have completely different values, it will return the “True” to the variable “new”. The value saved in the variable “new” will be displayed on the PowerShell using the “echo” statement as below.

You can also utilize the “Not Equal” operator within the “if-else” statement of PowerShell code. Hence, we declared two string variables and started an “If” statement. The “if” statement checks if two variables are not equal or equal using the “Not Equal” operator. If the condition is met and both variables turn out to be not equal, the first “echo” statement will be executed; otherwise, the else part will be executed. As both the variables are equal except case sensitivity, the conditions don’t get satisfied. In return, the second echo statement has been executed from the “else” part of the statement as per the output below.

Conclusion:

This guide explains using the “Not Equal” operator on characters, strings, and integer values within the PowerShell. We have enhanced the level of our examples by using more than 1 condition using the “OR” operator. We hope you liked it.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.