Operators
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).
Comparison Operators
Section titled “Comparison Operators”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 expressionsCollection 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
Section titled “Arithmetic Operators”1 + 2 # Addition1 - 2 # Subtraction-1 # Set negative value1 * 2 # Multiplication1 / 2 # Division1 % 2 # Modulus100 -shl 2 # Bitwise Shift-left100 -shr 1 # Bitwise Shift-rightAssignment Operators
Section titled “Assignment Operators”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 variableIncrement 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 1Redirection Operators
Section titled “Redirection Operators”Success output stream:
cmdlet > file # Send success output to file, overwriting existing contentcmdlet >> file # Send success output to file, appending to existing contentcmdlet 1>&2 # Send success and error output to error streamError output stream:
cmdlet 2> file # Send error output to file, overwriting existing contentcmdlet 2>> file # Send error output to file, appending to existing contentcmdlet 2>&1 # Send success and error output to success output streamWarning output stream: (PowerShell 3.0+)
cmdlet 3> file # Send warning output to file, overwriting existing contentcmdlet 3>> file # Send warning output to file, appending to existing contentcmdlet 3>&1 # Send success and warning output to success output streamVerbose output stream: (PowerShell 3.0+)
cmdlet 4> file # Send verbose output to file, overwriting existing contentcmdlet 4>> file # Send verbose output to file, appending to existing contentcmdlet 4>&1 # Send success and verbose output to success output streamDebug output stream: (PowerShell 3.0+)
cmdlet 5> file # Send debug output to file, overwriting existing contentcmdlet 5>> file # Send debug output to file, appending to existing contentcmdlet 5>&1 # Send success and debug output to success output streamInformation output stream: (PowerShell 5.0+)
cmdlet 6> file # Send information output to file, overwriting existing contentcmdlet 6>> file # Send information output to file, appending to existing contentcmdlet 6>&1 # Send success and information output to success output streamAll output streams:
cmdlet *> file # Send all output streams to file, overwriting existing contentcmdlet *>> file # Send all output streams to file, appending to existing contentcmdlet *>&1 # Send all output streams to success output streamDifferences to the pipe operator (|)
Redirection operators only redirect streams to files or streams to streams. The pipe operator pumps an object down the pipeline to a cmdlet or the output. How the pipeline works differs in general from how redirection works and can be read on Working with the PowerShell pipeline
Mixing operand types : the type of the left operand dictates the behavior.
Section titled “Mixing operand types : the type of the left operand dictates the behavior.”For Addition
"4" + 2 # Gives "42"4 + "2" # Gives 61,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 61,2,3 * 2 # Gives 1,2,3,1,2,32 * 1,2,3 # Gives an error op_Multiply is missingThe impact may have hidden consequences on comparison operators :
$a = Read-Host "Enter a number"Enter a number : 33$a -gt 5FalseLogical Operators
Section titled “Logical Operators”-and # Logical and-or # Logical or-xor # Logical exclusive or-not # Logical not! # Logical notString Manipulation Operators
Section titled “String Manipulation Operators”Replace operator:
The -replace operator replaces a pattern in an input value using a regular expression. This operator uses two arguments (separated by a comma): a regular expression pattern and its replacement value (which is optional and an empty string by default).
"The rain in Seattle" -replace 'rain','hail' #Returns: The hail in Seattle"kenmyer@contoso.com" -replace '^[\w]+@(.+)', '$1' #Returns: contoso.comSplit and Join operators:
The -split operator splits a string into an array of sub-strings.
"A B C" -split " " #Returns an array string collection object containing A,B and C.The -join operator joins an array of strings into a single string.
"E","F","G" -join ":" #Returns a single string: E:F:G