Search notes:

PowerShell: Operators

The subexpression operator $(…) is primarly used to evaluate an expression and incorporate its result into a string.
@(…) is the array subexpression operator: it creates an array from the expression within its parentheses, regardless if the expression evaluates to 0, 1 or more elements.
, with two variants (unary and binaray), both of which create arrays.
-not (two variants: logical and bitwise)
Arithmetic operators: +, - *, /, %, ++, --, +=, -=, *=, /=, -shl, -shr, -band, -bnot, -bor, -xor
Note that the forEach-object cmdlet can also be abbreviated with %.
String manipulation (-join, -split (unary and binary) and -replace)
The -as operator casts a value into a different type.
A value can also be cast by prepending a type before it: [int]0x40.
range operator. It creates an unconstrained one dimensional array. (see also ..)
The format operator (-f) formats strings.
-in and -notIn check if a value is (not) present in a collection.
The call (or invoke) operator & executes script files, script blocks, executables etc. with child scope. Thus, they cannot modify the current scope.
The dot sourcing operator (.) runs a script in the current scope and thus can add, delete or modify functions, aliases, variables etc.
logical: -and, -or, -xor, -not, !
assignment
The redirection operators (>, >>, 2>, 2>>, 2>&1) send a command's or an expression's output to a file.
Comparison operators (-eq, -gt, …)
PowerShell 7 also has the ternary operator ( $cond ? doSomething : doAnotherThing )

Order of precedence

$() / @() / ()
. Member access operator
:: Static operator
[…] Index operator
[datatype] cast operator
-split Split operator (This is a unary operator and has higher precedence than the binary split operator)
-join Join operator (This is a unary operator and has higher precedence than the binary join operator)
, Comma operator
++ / -- Increment and decrement (These operators are considered assignment operators, but have higher precedence than for example =, see below)
! / -not Logical operators
.. Range operator
-f Format operator
- Unary (negative) operator
* / / / %
+ / -
-split / -join / -is / -isNot / -as / -eq/ -ne / -ne / -gt / -gt / -lt / -le / -like / -notLike / -match / -notMatch / -in / -notIn / -contains / -notContains / -replace The -split and -join operator with this precedence are binary opeators. There is also a unary version that has higher precedence.
-band / -bnot / -bor / -bxor / -shl / shr Bitwise operators
-and / -or -xor
. Dot source operator
& Call operator
| Pipeline operator
> / >> / 2> / 2>> / 2>&1 Redirection operators
= / += / -= *= / /= / %= Assignment, compare with ++ and --

Setting the color used for operators

PowerShell allows to define a color that is used for operators when using an interactive shell:
set-psReadLineOption -colors @{
   operator = 
      "$([char]27)[38;2;255;127;0m"   +  # forground color: orange
      "$([char]27)[48;2;100;120;130m"    # background color: grey
}

Experimental operators

PowerShell Core uses experimental features to introduce some experimental operators:
These operators can be enabled with
PS C:\> enable-experimentalFeature PSTernaryOperator
PS C:\> enable-experimentalFeature PSPipelineChainOperators

Misc

Operators cannot be aliased.

See also

The PowerShell language

Index