Comparison operators

I. Introduction

Comparison operators are at the heart of many languages, and PowerShell is no exception. They enable you to compare values, which is essential and very common in scripts, particularly within conditional structures. In this chapter, we'll explore the various comparison operators available in PowerShell.

II. Equality operators

Among the many comparison operators, there are the so-called equality operators. These allow us to check whether one value is equal to another. In PowerShell, the equality operator is "-eq". Incidentally, this is the equivalent of the "==" operator in other languages. On the other hand, to verify inequality and check whether two values are indeed different, we need to use the"-ne" operator.

When we compare two values, the result is returned as a Boolean: true or false.

You can open a console and run a few tests, like those shown in the following image. By the way, these examples make one thing clear: by default, PowerShell is case-insensitive, i.e. it doesn't differentiate between upper and lower case. For it, "PowerShell" is equal to "POWERSHELL", just as "PowerShell" is equal to "PowerShell", because it returns "True".

PowerShell - Equality operator example

If we want the comparison test to be case-sensitive, then we need to add the "c" character after the dash and before the operator name. Thus, the operator "-eq" becomes "-ceq". The same applies to all other operators. If we run similar tests with this operator, we can see that the last two comparisons return "False" this time.

Note : to explicitly indicate that the comparison test must be case-insensitive, you must specify the "i" character, on the same principle as the "c" character. Thus, the "-eq" operator becomes "-ieq".

PowerShell - Compare and case sensitive

We don't have this problem when comparing numbers, but it's an important detail for strings.

III. Comparison operators: superior or inferior

PowerShell also provides operators for checking whether a value is greater than or less than another :

OperatorDescription
-gt
Greater than
More than...
-ge
Greater or equal
Greater than or equal to...
-lt
Less than
Less than....
-
Less or equel
Less than or equal to...

Again, you can add a "c" between the hyphen and the operator name for case-sensitivity.

PowerShell - Top or bottom operator

IV. Comparison operators: similarity

Among PowerShell's comparison operators, there are those that allow you to search for a more or less close match between two values. The idea is to search for the presence of a character string in another string, without the latter being exactly the same. In other words, it can be present without representing the complete string.

OperatorDescription
-likeCorresponds to...
-notlikeDoes not correspond to...

Here again, the test returns true or false depending on the result. Here are a few examples:

PowerShell - Like operator example

We can see that "PowerShell" corresponds to "Power*", but not to "ower*", nor to "ower", although there is a resemblance. The power of the "-like" and "-notlike" operators lies in their use of the "*" ( wildcard) and "?" characters.

  • The"*" character can be replaced by any character, whether 1 or more.
  • The "?" character can be replaced by any character, but only one character.

When PowerShell tells us that "PowerShell" does not correspond to "ower*", this is normal. For him, the first character of the string must be "o", not "P". If we want it to consider "P", we need to add a substitution character ("*" or "?").

We can see the difference:

The "-like" operator can be very useful in a conditional structure (if/else) or in a filter with Where-Object. For example, if you are looking for all the files in the "D:Documents" directory whose name contains the keyword "Tutorial", you can use this command:

Get-ChildItem -Path "D:Documents" | Where-Object { $_.Name -like "*Tutoriel*" }

The"-NotLike" operator works on the same principle, but in reverse ("does not match").

To go further and create more complex correspondence models, you can study these two operators:

OperatorDescription
-matchCorresponds to the regular expression...
-notmatchDoes not match the regular expression...

These operators accept matching patterns, which correspond to regular expressions (RegEx). Although very powerful, this is not an easy concept to grasp.

If you want to learn more about this concept later, read this article:

V. Comparison operators: contains or does not contain

Finally, we'll look at 4 comparison operators whose aim is to check whether a value is present in a collection, or conversely, whether the collection contains the value. The term collection refers to a set of values. It's all a question of logic, really.

Here are the 4 operators we are referring to:

OperatorDescription
-containsThe collection contains the value
-notcontainsThe collection does not contain the value
-inThe value is present in the
-notinThe value is not present in the collection

We're going to run a few tests with these operators to help you understand how they work.

First, we'll create an array with several values:

$Langages = @("PowerShell","Python","Bash","MS-DOS")

Next, we want to check whether the value "PowerShell" is present in the array stored in the $Languages variable. We can use "-contains" and"-in" to do this.

$Langages -contains "PowerShell"

Then, with the second operator :

"PowerShell" -in $Langages

Each time, we get the result "True", because this is the case: PowerShell is present in the table of values. Nevertheless, be careful about where you place your values so as not to get an unexpected result.

Translating the test we're trying to perform can help. For example:

$Langages -contains "PowerShell"

The result is:"Does $Langages contain PowerShell?", which corresponds to the test we wish to perform. If we replace "-contains" with "-in" without inverting the variables, this would give:"Is $Langages present in PowerShell?" - Something's wrong here, which is why the two variables must be inverted.

Instead of using a static array, we can obtain this collection from the result of a command. For example, if we store the list of local users in a variable, we can then search for the presence of a user in this list.

$Utilisateurs = (Get-LocalUser).Name
"Florian" -in $Utilisateurs
# ou
$Utilisateurs -contains "Florian"

VI. Conclusion

You are now familiar with the various comparison operators available in PowerShell. They can be used in a number of situations, notably when writing a filter with "Where-Object" or in a conditional structure (if/else).