PowerShell


Operators All Versions

1.0
2.0
3.0
4.0
5.0
5.1 Preview

This draft deletes the entire topic.

expand all collapse all

Examples

  • 7

    PowerShell comparison operators are comprised of a leading dash (-) followed by a name (eq for equal, gt for greater than, etc...).

    Names can be preceded by special characters to modify the behavior of the operator:

    i # Case-Insensitive Explicit (-ieq)
    c # Case-Sensitive Explicit (-ceq)
    

    Case-Insensitive is the default if not specified, ("a" -eq "A") same as ("a" -ieq "A").

    Simple comparison operators:

    2 -eq 2    # Equal to (==)
    2 -ne 4    # Not equal to (!=)
    5 -gt 2    # Greater-than (>)
    5 -ge 5    # Greater-than or equal to (>=)
    5 -lt 10   # Less-than (<)
    5 -le 5    # Less-than or equal to (<=)
    

    String comparison operators:

    "MyString" -like "*String"            # Match using the wildcard character (*)
    "MyString" -notlike "Other*"          # Does not match using the wildcard character (*)
    "MyString" -match "$String^"          # Matches a string using regular expressions
    "MyString" -notmatch "$Other^"        # Does not match a string using regular expressions
    "Get-Process" -replace "Get", "Stop"  # Changes the specified elements of a value
    

    Collection comparison operators:

    "abc", "def" -contains "def"            # Returns true when the value (right) is present
                                            # in the array (left)
    "abc", "def" -notcontains "123"         # Returns true when the value (right) is not present
                                            # in the array (left)
    "def" -in "abc", "def"                  # Returns true when the value (left) is present
                                            # in the array (right)
    "123" -notin "abc", "def"               # Returns true when the value (left) is not present
                                            # in the array (right)
    
  • 5
    1 + 2      # Addition
    1 - 2      # Subtraction
    -1         # Set negative value
    1 * 2      # Multiplication
    1 / 2      # Division
    1 % 2      # Modulus
    100 -shl 2 # Bitwise Shift-left
    100 -shr 1 # Bitwise Shift-right
    
  • 4

    Simple arithmetic:

    $var = 1      # Assignment. Sets the value of a variable to the specified value
    $var += 2     # Addition. Increases the value of a variable by the specified value
    $var -= 1     # Subtraction. Decreases the value of a variable by the specified value
    $var *= 2     # Multiplication. Multiplies the value of a variable by the specified value
    $var /= 2     # Division. Divides the value of a variable by the specified value
    $var %= 2     # Modulus. Divides the value of a variable by the specified value and then
                  # assigns the remainder (modulus) to the variable
    

    Increment and decrement:

    $var++   # Increases the value of a variable, assignable property, or array element by 1
    $var--   # Decreases the value of a variable, assignable property, or array element by 1
    

I am downvoting this example because it is...

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Operators? Ask Question

Comparison Operators

7

PowerShell comparison operators are comprised of a leading dash (-) followed by a name (eq for equal, gt for greater than, etc...).

Names can be preceded by special characters to modify the behavior of the operator:

i # Case-Insensitive Explicit (-ieq)
c # Case-Sensitive Explicit (-ceq)

Case-Insensitive is the default if not specified, ("a" -eq "A") same as ("a" -ieq "A").

Simple comparison operators:

2 -eq 2    # Equal to (==)
2 -ne 4    # Not equal to (!=)
5 -gt 2    # Greater-than (>)
5 -ge 5    # Greater-than or equal to (>=)
5 -lt 10   # Less-than (<)
5 -le 5    # Less-than or equal to (<=)

String comparison operators:

"MyString" -like "*String"            # Match using the wildcard character (*)
"MyString" -notlike "Other*"          # Does not match using the wildcard character (*)
"MyString" -match "$String^"          # Matches a string using regular expressions
"MyString" -notmatch "$Other^"        # Does not match a string using regular expressions
"Get-Process" -replace "Get", "Stop"  # Changes the specified elements of a value

Collection comparison operators:

"abc", "def" -contains "def"            # Returns true when the value (right) is present
                                        # in the array (left)
"abc", "def" -notcontains "123"         # Returns true when the value (right) is not present
                                        # in the array (left)
"def" -in "abc", "def"                  # Returns true when the value (left) is present
                                        # in the array (right)
"123" -notin "abc", "def"               # Returns true when the value (left) is not present
                                        # in the array (right)

Arithmetic Operators

5
1 + 2      # Addition
1 - 2      # Subtraction
-1         # Set negative value
1 * 2      # Multiplication
1 / 2      # Division
1 % 2      # Modulus
100 -shl 2 # Bitwise Shift-left
100 -shr 1 # Bitwise Shift-right

Assignment Operators

4

Simple arithmetic:

$var = 1      # Assignment. Sets the value of a variable to the specified value
$var += 2     # Addition. Increases the value of a variable by the specified value
$var -= 1     # Subtraction. Decreases the value of a variable by the specified value
$var *= 2     # Multiplication. Multiplies the value of a variable by the specified value
$var /= 2     # Division. Divides the value of a variable by the specified value
$var %= 2     # Modulus. Divides the value of a variable by the specified value and then
              # assigns the remainder (modulus) to the variable

Increment and decrement:

$var++   # Increases the value of a variable, assignable property, or array element by 1
$var--   # Decreases the value of a variable, assignable property, or array element by 1

Redirection Operators

4

Success output stream:

cmdlet > file     # Send success output to file, overwriting existing content
cmdlet >> file    # Send success output to file, appending to existing content
cmdlet 1>&2       # Send success and error output to error stream

Error output stream:

cmdlet 2> file    # Send error output to file, overwriting existing content
cmdlet 2>> file   # Send error output to file, appending to existing content
cmdlet 2>&1       # Send success and error output to success output stream

Warning output stream: (PowerShell 3.0+)

cmdlet 3> file    # Send warning output to file, overwriting existing content
cmdlet 3>> file   # Send warning output to file, appending to existing content
cmdlet 3>&1       # Send success and warning output to success output stream

Verbose output stream: (PowerShell 3.0+)

cmdlet 4> file    # Send verbose output to file, overwriting existing content
cmdlet 4>> file   # Send verbose output to file, appending to existing content
cmdlet 4>&1       # Send success and verbose output to success output stream

Debug output stream: (PowerShell 3.0+)

cmdlet 5> file    # Send debug output to file, overwriting existing content
cmdlet 5>> file   # Send debug output to file, appending to existing content
cmdlet 5>&1       # Send success and debug output to success output stream

Information output stream: (PowerShell 5.0+)

cmdlet 6> file    # Send information output to file, overwriting existing content
cmdlet 6>> file   # Send information output to file, appending to existing content
cmdlet 6>&1       # Send success and information output to success output stream 

All output streams:

cmdlet *> file    # Send all output streams to file, overwriting existing content
cmdlet *>> file   # Send all output streams to file, appending to existing content
cmdlet *>&1       # Send all output streams to success output stream

Logical Operators

3
-and # Logical and
-or  # Logical or
-xor # Logical exclusive or
-not # Logical not
!    # Logical not

Mixing operand types : the type of the left operand dictates the behavior.

3

For Addition

"4" + 2         # Gives "42"
4 + "2"         # Gives 6
1,2,3 + "Hello" # Gives 1,2,3,"Hello"
"Hello" + 1,2,3 # Gives "Hello1 2 3"

For Multiplication

"3" * 2   # Gives "33"
2 * "3"   # Gives 6
1,2,3 * 2 # Gives 1,2,3,1,2,3
2 * 1,2,3 # Gives an error op_Multiply is missing

The impact may have hidden consequences on comparison operators :

$a = Read-Host "Enter a number"
Enter a number : 33
$a -gt 5
False

Topic Outline