Operators

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

    PowerShell provides a variety of operators to perform operations on values and variables. Here's a list of common operators in PowerShell, grouped by their function:

    Arithmetic operators:

    +: Addition
    -: Subtraction
    *: Multiplication
    /: Division
    %: Modulus (remainder of division)

    Assignment operators:

    =: Assign a value to a variable
    +=: Add and assign (increment)
    -=: Subtract and assign (decrement)
    *=: Multiply and assign
    /=: Divide and assign
    %=: Modulus and assign


    Comparison operators:

    -eq: Equal to
    -ne: Not equal to
    -gt: Greater than
    -ge: Greater than or equal to
    -lt: Less than
    -le: Less than or equal to


    Logical operators:

    -and: Logical AND
    -or: Logical OR
    -not or !: Logical NOT
    -xor: Logical exclusive OR


    Bitwise operators:

    -band: Bitwise AND
    -bor: Bitwise OR
    -bnot: Bitwise NOT
    -bxor: Bitwise exclusive OR
    -shl: Bitwise shift left
    -shr: Bitwise shift right


    Redirection operators:

    >: Redirect output to a file (overwrite)
    >>: Redirect output to a file (append)
    2>: Redirect error output to a file (overwrite)
    2>>: Redirect error output to a file (append)


    Pipeline operator:

    |: Pass the output of one command as input to another command


    String operators:

    -join: Concatenate elements of an array into a single string
    -split: Split a string into an array based on a delimiter
    -replace: Replace a pattern in a string with a specified value
    -match: Test if a string matches a regular expression pattern
    -notmatch: Test if a string does not match a regular expression pattern


    Type operators:

    -is: Test if a value is of a specific type
    -isnot: Test if a value is not of a specific type
    -as: Attempt to cast a value to a specific type


    These operators allow you to perform various operations on values and variables in PowerShell, enabling you to create more complex scripts and commands. For a complete list of PowerShell operators and their descriptions, consult the official PowerShell documentation.

    Examples:

    Comparision 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:>

    String 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