PowerShell Comparison Operators


Comparison operators let is used for comparing values and finding values that match with particular patterns. To use a comparison operator, specify the values that you want to compare together with an operator. 

Equality Operators

The following are equality operators 
-eq equals
-ne not equals
-gt greater than
-ge greater than or equal
-lt less than
-le less than or equal

Equality operators will return a value of TRUE when one or more of the input values is identical to the specified pattern. The entire pattern must match an entire value

"-eq, -ne, -lt, -le, -gt, -ge"
Write-Host (2 -eq 2)                # True
Write-Host (2 -eq 3)                # False
Write-Host (1, 2, 3 -eq 2)          # return 2
Write-Host ("abc" -eq "abc")        # True
Write-Host ("abc" -eq "abc", "def") # False
Write-Host ("abc", "def" -eq "abc") # return abc
Write-Host ("abc" -ne "def")        # True
Write-Host ("abc" -ne "abc")        # False
Write-Host ("abc" -ne "abc", "def") # True
Write-Host ("abc", "def" -ne "abc") # def
Write-Host (3 -lt 4)                # True
Write-Host (1, 2, 3, 4, 5 -lt 5)    # 1 2 3 4
Write-Host (3 -le 3)                # True
Write-Host (1, 2, 3, 4 -le 4)       # 1 2 3 4
Write-Host (8 -gt 6)                # True
Write-Host (1, 2, 3, 4, 5 -gt 2)    # 3 4 5
Write-Host (2, 3, 4 -ge 2)          # 2 3 4
Write-Host (9 -ge 9)                # True

Matching Operators

The following are matching 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
 
 
"-like, -notlike, -match, -notmatch"
Write-Host ("PowerShell" -like "*shell")                    # True
Write-Host ("PowerShell", "Server" -like "*shell")          # PowerShell
Write-Host ("PowerShell" -notlike "*shell")                 # False
Write-Host ("PowerShell", "Server" -notlike "*shell")       # Server
Write-Host ("Sunday", "Monday", "Tuesday" -match "day")     # Sunday Monday Tuesday
Write-Host ("Sunday" -notmatch "day")                       # False
Write-Host ("Sunday" -notmatch "rain")                      # True
Write-Host ("July", "September", "October" -notmatch "ber") # July

Containment Operators

The following are containment 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 acollection
-notin Returns true when test value not contained in a collection

"-contain, -notcontain, -in, -notin"
# <reference-value> -contains/-notcontains <test-value>
# it will return TRUE at least one of the refrence values matched/notmatched
Write-Host ("abc", "def" -contains "def") # True
Write-Host ("Windows", "PowerShell" -contains "Shell") # False
Write-Host ("abc", "def", "ghi" -contains "abc", "def") # False
$a = "abc", "def"
Write-Host ("abc", "def", "ghi" -contains $a) # False
Write-Host ($a, "ghi" -contains $a) # True
Write-Host ("abc", "def", "ghi" -notcontains "abc", "def") # true
Write-Host ("abc", "def", "ghi" -notcontains $a) # True
Write-Host ($a, "ghi" -notcontains $a) # False
# <test-value> -in/-notin <reference-values>
Write-Host ("def" -in "abc", "def") # true
Write-Host ("Shell" -in "Windows", "PowerShell") # False
Write-Host ("Windows" -in "Windows", "PowerShell") # True
Write-Host ("Windows", "PowerShell" -in "Windows", "PowerShell", "ServerManager") # False
$a = "Windows", "PowerShell"
Write-Host ($a -in $a, "ServerManager") # True
Write-Host ("def" -notin "abc", "def") # False
Write-Host ("ghi" -notin "abc", "def") # True
Write-Host ("Shell" -notin "Windows", "PowerShell") # True
Write-Host ("Windows" -notin "Windows", "PowerShell") # False

Replacement Operators

"-replace"
Write-Host ("book" -replace "B", "C") # Cook
Write-Host ("book" -ireplace "B", "C") # Cook , i means case insensitive
Write-Host ("book" -creplace "B", "C") # book , c means case sensitive

Type Comparison

"Type comparison -is, -isnot"
# <object> -is/-isnot <type reference>
$a = 1
$b = "1"
$a -is [int] # True
$a -is $b.GetType() # False
$a -isnot $b.GetType() # True
$a -isnot [int] # False
$b -isnot [int] # True

Subscribe to receive free email updates:

0 Response to "PowerShell Comparison Operators"

Post a Comment