Operators and expressions – Arithmetic, assignment, comparison, pattern matching logical and bit wise operators

    PowerShell includes the following comparison operators:

    Operator Type Operators Description
    Equality -eq
    -ne
    -gt
    -ge
    -lt
    -le
    equals
    not equals
    greater than
    greater than or equal
    less than
    less than or equal
    Matching -like
    -notlike
    -match
    -notmatch
    Returns true when string matches wildcard pattern
    Returns true when string does not match wildcard pattern
    Returns true when string matches regex pattern – $matches contains matching strings
    Returns true when string does not match regex pattern – $matches contains matching strings
    Containment -contains
    -notcontains
    -in
    -notin
    Returns true when reference value contained in a collection
    Returns true when reference value not contained in a collection
    Returns true when test value contained in a collection
    Returns true when test value not contained in a collection
    Replacement -replace replace a string pattern
    Type comparison -is
    -isnot
    Returns true if both object are the same type
    Returns true if the objects are not the same type

    Examples:

    Equality Operators:

    PS C:\> 2 -eq 2
    True
    PS C:\> 2 -eq 3
    False
    PS C:\> 2 -ne 3
    True
    PS C:\> 2 -ne 2
    False

    PowerShell operators are by default not case sensitive.

    PS C:\> "PowerShell" -eq "PowerShell"
    True
    PS C:\> "PowerShell" -eq "POWERSHELL"
    True

    If you want to make case sensitive, then we have to use -ceq.

    PS C:\> "PowerShell" -ceq "POWERSHELL"
    False
    PS C:\> 10 -gt 10
    False
    PS C:\> 10 -gt 11
    False
    PS C:\> 100 -gt 10
    True
    PS C:\> 10 -gt 100
    False
    PS C:\> 10 -ge 10
    True
    PS C:\> 100 -ge 10
    True
    PS C:\> 10 -lt 100
    True
    PS C:\> 100 -lt 10
    False
    PS C:\> 10 -le 100
    True
    PS C:\> 100 -le 10
    False
    PS C:\>

    Matching Operators:

    PS C:\> "WindowsServer2012" -like "Windows"
    False
    PS C:\> "WindowsServer2012" -like "Windows*"
    True
    PS C:\> "WindowsServer2012" -like "*Server2012"
    True
    PS C:\> "WindowsServer2012" -like "*Server*"
    True
    PS C:\> "WindowsServer2012" -match "Server"
    True
    PS C:\> "WindowsServer2012" -match "2012"
    True
    PS C:\> "WindowsServer2012" -match "Windows"
    True
    PS C:\> "WindowsServer2012" -cmatch "Windows"
    True
    PS C:\> "WindowsServer2012" -cmatch "WINDOWS"
    False
    PS C:\> "WindowsServer2012" -notmatch "WINDOWS"
    False
    PS C:\> "WindowsServer2012" -cnotmatch "WINDOWS"
    True

    Containment Operators:

    PS C:\> "abc", "def" -Contains "def"
    True
    PS C:\> "Windows", "PowerShell" -Contains "Shell"
    False
    PS C:\> "Windows", "PowerShell" -Contains "*Shell"
    False

    Replacement:

    PS C:\> $a = "Windows Server 2012"
     
    PS C:\> $a -replace "2012","2016"
    Windows Server 2016

    Type Comparison:

    PS C:\> 123 -is [int]
    True
     
    PS C:\> "Red" -is [string]
    True
     
    PS C:\> $a = 1,2,3,4,5
    PS C:\> $a -is [array]
    True

     


     

    Page 5 of 18

    © 2020 WinAdmin.org, All Rights Reserved.

    We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.