PowerShell


Operators All Versions

1.0
2.0
3.0
4.0
5.0
5.1 Preview

This draft deletes the entire topic.

Introduction

An operator is a character that represents an action. It tells the compiler/interpreter to perform specific mathematical, relational or logical operation and produce final result. PowerShell interprets in a specific way and categorizes accordingly like arithmetic operators perform operations primarily on numbers, but they also affect strings and other data types. Along with the basic operators,PowerShell has a number of operators that save time and coding effort(eg: -like,-match,-replace,etc).

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
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Operators? Ask Question

Topic Outline