PowerShell Comparison Operators
This tutorial will provide you information about PowerShell Comparison Operators. Comparison operators are used widely when you are writing your scripts. They will help you to test, compare, find, modify replace data and information. The comparison operators, as per Microsoft, are divided into those five types.
Types of comparison operators:
- Equality
- Matching
- Containment
- Replacement
- Type
Each type of comparison operator has different purpose. We will check what each type of comparison operators are doing in more details and which are those operators. On each category we will also see few examples that may help you to understand better their use.
Comparison Operators List
Equality comparison operators
Equality comparison operators will check if two values are equal, and also if one values is less or greater than the other value. If a value is checked against a collection and a match is found the equality operators will not stop until they will find all values in the collection. Below is the list of the equality comparison operators:
- -eq – Equals
- -ne – Not equals
- -gt – Greater than
- -ge – Greater than or equal
- -lt – Less than
- -le – Less than or equal
Examples
-eq
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $a -eq $b
True
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -eq $b
False
PS S:\> $a = "aaa"
PS S:\> $b = "aaa"
PS S:\> $a -eq $b
True
PS S:\> $a = "aaa"
PS S:\> $b = "bbb"
PS S:\> $a -eq $b
False
PS S:\> $a = "aaa","bbb","ccc"
PS S:\> $b = "aaa"
PS S:\> $a -eq $b
aaa
PS S:\> $a = "aaa","bbb","ccc"
PS S:\> $b = "aaa"
PS S:\> $b -eq $a
False
PS S:\> $a = 1
PS S:\> $b = "bbb"
PS S:\> $a -eq $b.count
True
PS S:\> $a = 3
PS S:\> $b = "bbb"
PS S:\> $a -eq $b.count
False
-ne
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $a -ne $b
False
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -ne $b
True
PS S:\> $a = "aaa"
PS S:\> $b = "aaa"
PS S:\> $a -ne $b
False
PS S:\> $a = "aaa"
PS S:\> $b = "bbb"
PS S:\> $a -ne $b
True
PS S:\> $a = "aaa","bbb","ccc"
PS S:\> $b = "aaa"
PS S:\> $a -ne $b
bbb
ccc
PS S:\> $a = "aaa","bbb","ccc"
PS S:\> $b = "aaa"
PS S:\> $b -ne $a
True
PS S:\> $a = 1
PS S:\> $b = "bbb"
PS S:\> $a -ne $b.count
False
PS S:\> $a = 3
PS S:\> $b = "bbb"
PS S:\> $a -ne $b.count
True
-gt
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $a -gt $b
False
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $b -gt $a
False
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -gt $b
False
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $b -gt $a
True
PS S:\> $a = 1,2,3,4,5
PS S:\> $b = 3
PS S:\> $a -gt $b
4
5
-ge
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $a -ge $b
True
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $b -ge $a
True
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -ge $b
False
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $b -ge $a
True
PS S:\> $a = 1,2,3,4,5
PS S:\> $b = 3
PS S:\> $a -ge $b
3
4
5
-lt
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $a -lt $b
False
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $b -lt $a
False
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -lt $b
True
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $b -lt $a
False
PS S:\> $a = 1,2,3,4,5
PS S:\> $b = 3
PS S:\> $a -lt $b
1
2
-le
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $a -le $b
True
PS S:\> $a = 1
PS S:\> $b = 1
PS S:\> $b -le $a
True
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -le $b
True
PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $b -le $a
False
PS S:\> $a = 1,2,3,4,5
PS S:\> $b = 3
PS S:\> $a -le $b
1
2
3
Matching comparison operators
Matching comparison operators will return true or false as an answer to the check of a variable. If you will use -match
or -notmatch
, the matching strings are saved under a default variable called $matches
, which includes those values. Based on the operator that you will use, you can use different ways to perform the check. Below is the list of matching comparison operators:
- -like – Returns true when string matches wildcard pattern
- -notlike – Returns true when string does not match wildcard pattern
- -match – Returns true when string matches regex pattern;
$matches
contains matching strings - -notmatch – Returns true when string does not match regex pattern;
$matches
contains matching strings
Examples
-like
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -like "*a"
aaaaa
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -like "a*"
aaaaa
aaabb
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -like "*a*"
aaaaa
aaabb
baaab
-notlike
Note lines 10 and 15
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notlike "*aaa"
bbbbb
ccccc
ddddd
eeeee
aaabb
bbbcc
cccdd
dddee
eeeff
baaab
cbbbc
dcccd
eddde
feeef
Note line 14
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notlike "aaa*"
bbbbb
ccccc
ddddd
eeeee
bbbcc
cccdd
dddee
eeeff
baaab
cbbbc
dcccd
eddde
feeef
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notlike "*aaa*"
bbbbb
ccccc
ddddd
eeeee
bbbcc
cccdd
dddee
eeeff
cbbbc
dcccd
eddde
feeef
-match
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -match "aaa"
aaaaa
aaabb
baaab
PS S:\> $a = "baaaaab"
PS S:\> $a -match "aaa"
True
PS S:\> $Matches
Name Value
---- -----
0 aaa
PS S:\> $a = "baaaaab"
PS S:\> $a -match "bbb"
False
–notmatch
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notmatch "aaa"
bbbbb
ccccc
ddddd
eeeee
bbbcc
cccdd
dddee
eeeff
cbbbc
dcccd
eddde
feeef
PS S:\> $a = "baaaaab"
PS S:\> $a -notmatch "aaa"
False
PS S:\> $a = "baaaaab"
PS S:\> $a -notmatch "bbb"
True
Containment comparison operators
Containment comparison operators will check a reference value if it is contained in a collection. These operators are similar to equality operators. One of differences is that the return value is always a Boolean value. The other difference is that the operator will stop the check against a collection as soon as it finds the first match. Below is the list of containment comparison operators:
- -contains – Returns true when reference value contained in a collection
- -notcontains – Returns true when reference value not contained in a collection
- -in – Returns true when test value contained in a collection
- -notin – Returns true when test value not contained in a collection
Examples
-contains
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -contains "aaaaa"
True
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -contains "aaaab"
False
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -contains "aaa"
False
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -contains "aaaaa","bbbbb"
False
-notcontains
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notcontains "aaaaa"
False
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notcontains "aaaab"
True
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notcontains "aaa"
True
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> $a -notcontains "aaaaa","bbbbb"
True
-in
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaaaa" -in $a
True
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaaab" -in $a
False
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaa" -in $a
False
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaaaa","bbbbb" -in $a
False
-notin
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaaaa" -notin $a
False
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaaab" -notin $a
True
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaa" -notin $a
True
PS S:\> $a = "aaaaa","bbbbb","ccccc","ddddd","eeeee",
"aaabb","bbbcc","cccdd","dddee","eeeff",
"baaab","cbbbc","dcccd","eddde","feeef"
PS S:\> "aaaaa","bbbbb" -notin $a
True
Replacement comparison operators
Replacement comparison operator will replace the value or part of a value according to the regular expression that you will use. The operator by default is case-insensitive. There are to ways to force the behaviour of the operator. One is to use c in front of it to make it case-sensitive (-creplace
). The other way is to use i in front of it to force its behaviour to be case-insensitive (-ireplace
). Below is the only replacement comparison operators that can be used:
- -replace – Replaces a string pattern
Examples
-replace
PS S:\> "aBcDe" -replace "a","b"
bBcDe
PS S:\> "aBcDe" -replace "a","B"
BBcDe
PS S:\> "aBcDe" -replace "A","b"
bBcDe
PS S:\> "aBcDe" -replace "A","B"
BBcDe
-creplace
PS S:\> "aBcDe" -creplace "a","b"
bBcDe
PS S:\> "aBcDe" -creplace "a","B"
BBcDe
PS S:\> "aBcDe" -creplace "A","b"
aBcDe
PS S:\> "aBcDe" -creplace "A","B"
aBcDe
-ireplace
PS S:\> "aBcDe" -ireplace "a","b"
bBcDe
PS S:\> "aBcDe" -ireplace "a","B"
BBcDe
PS S:\> "aBcDe" -ireplace "A","b"
bBcDe
PS S:\> "aBcDe" -ireplace "A","B"
BBcDe
Type comparison operators
Type comparison operators will check if the type of an object is the same as the defined or as another object.. Below is the list of type comparison operators:
- -is – Returns true if both object are the same type
- -isnot – Returns true if the objects are not the same type
Examples
-is
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $a -is [int]
True
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $a -is [string]
False
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $b -is [string]
True
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $a -is $b.GetType()
False
PS S:\> $a = "6"
PS S:\> $b = "5"
PS S:\> $a -is $b.GetType()
True
-isnot
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $a -isnot [int]
False
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $a -isnot [string]
True
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $b -isnot [string]
False
PS S:\> $a = 5
PS S:\> $b = "5"
PS S:\> $b -isnot [string]
False
PS S:\> $a = "6"
PS S:\> $b = "5"
PS S:\> $a -isnot $b.GetType()
False
Summary
Comparison Operators will help us filter out results when we are trying to retrieve information from other systems and get the exact results that we would like to while we are writing our scripts. I know that this tutorial has a lot of code examples and might confuse you. Read it carefully and it will help you. It is good also to test the behaviour of the operators yourself also, as practicing will help you understand them much easier. Comparison operators are not a difficult subject but it is really important to know them for future use in your scripts.
I hope the tutorial about comparison operators is helpful.
Please let me know your comments and thoughts. You feedback is appreciated.
Related Links:
- PowerShell Tutorials
- PowerShell Scripts
- about_Comparison_Operators | Microsoft Docs
- PowerShell Logical Operators


[…] on April 16, 2018by admin submitted by /u/SConstantinou [link] [comments] No comments […]