Search notes:
PowerShell: Operators
@(…) 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
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.
assignment
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
}