Hi, Damian Garbus here and this is a new blog post. I have written an article about Powershell conditions a few weeks ago. It’s time for a post about Powershell Comparison Operators. In my opinion, it’s one of the most important fundamentals of Powershell. Don’t waste time and let’s start.
What are Powershell Comparison Operators?
Powershell is based on objects. Comparision operators allow you to compare Powershell objects values. In result, you can determine true or false. For example, if you want to check two variables has the same content, Powershell tells you true or false.
List of Powershell Comparison Operators
Using Powershell, you can compare the entire value or parts of it.
Entire Value Comparison
First I will describe operators that compare entire values. It’s very necessary to understand variable types. If you compare integer numbers, Powershell compares their values. For a string, Powershell will check if all letters and order are the same.
–eq (equal) – check if values are equal (the same).
-ne (not equal) – check if values are not equal (not the same)
-gt (greater than) – check if the first number is greater than second
-lt (less than) – check if the first number is less than second
-le (less or equal) – check if the first number is less or equal than second
-ge (greater or equal) – check if the first number is greater or equal than second
"Poshland" -eq "Posh" "Poshland" -ne "Posh" 4 -eq 4 4 -gt 4 4 -ge 4 4 -lt 4 4 -le 4
-like – This operator checks if the second value contains first one. In this case, you must use * as a wildcard. In the future posts, I will write about Powershell wildcards. Now, you can check here or ask me via mail.
-notlike – opposite to ‘-like’ operator. See example.
-match – Similar to like but in background use the regular expression and you don’t have to use the wildcards.
-notmatch – Opposite to ‘-match’. See example.
"Poshland Blog" -like "Poshland*" "Poshland Blog" -notlike "Poshland*" "Poshland Blog" -match "Poshland" "Poshland Blog" -notmatch "Poshland"
-contains – check if the first value contains the second one. You can use it with array.
-notcontains – opposite to ‘-contains’. See example.
-in – similar to ‘-contains’. You can check if the first value is included in the second value.
-notin – opposite to ‘-in’. See example.
$array = "Poshland","Blog" $array $array -contains "Poshland" $array -notcontains "Poshland" "Poshland" -in $array "Poshland" -notin $array
As you can see, to learn and understand Powershell you have to understand one by one part of knowledge. To understand Powershell Comparison Operators, you have to understand variable types, wildcards, regular expression, and arrays. But remember I want to write about this steps one by one and help you learn Powershell. Please let me know about your experience with learning Powershell. Share your Powershell learning methods.