PowerShell Operators
Arithmetic Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
+ | Adds two numbers or concatenates strings. | 5 + 3 | 8 |
- | Subtracts one number from another. | 10 - 4 | 6 |
* | Multiplies two numbers. | 6 * 7 | 42 |
/ | Divides one number by another. | 15 / 3 | 5 |
% | Returns the remainder of a division operation. | 10 % 3 | 1 |
++ | Increments a variable’s value by one (post-increment). | $a++ (if $a = 5 ) | $a = 6 |
-- | Decrements a variable’s value by one (post-decrement). | $b-- (if $b = 5 ) | $b = 4 |
Assignment Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
= | Assigns a value to a variable. | $a = 10 | $a = 10 |
+= | Adds and assigns the value to a variable. | $a += 5 | $a = 15 (if $a = 10 ) |
-= | Subtracts and assigns the value to a variable. | $a -= 3 | $a = 7 (if $a = 10 ) |
*= | Multiplies and assigns the value to a variable. | $a *= 2 | $a = 20 (if $a = 10 ) |
/= | Divides and assigns the value to a variable. | $a /= 2 | $a = 5 (if $a = 10 ) |
%= | Modulo and assigns the value to a variable. | $a %= 4 | $a = 2 (if $a = 10 ) |
Comparison Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
-eq | Checks if two values are equal. | 5 -eq 5 | True |
-ne | Checks if two values are not equal. | 5 -ne 3 | True |
-gt | Checks if the left value is greater than the right. | 10 -gt 5 | True |
-ge | Checks if the left value is greater than or equal to the right. | 5 -ge 5 | True |
-lt | Checks if the left value is less than the right. | 3 -lt 10 | True |
-le | Checks if the left value is less than or equal to the right. | 5 -le 10 | True |
-like | Matches a string with a wildcard pattern. | 'PowerShell' -like '*Shell*' | True |
-notlike | Does not match a string with a wildcard pattern. | 'PowerShell' -notlike 'Cmd*' | True |
-match | Matches a string with a regular expression. | 'abc123' -match '\d{3}' | True |
-notmatch | Does not match a string with a regular expression. | 'abc123' -notmatch '\d{4}' | True |
-contains | Checks if a collection contains a value. | @(1,2,3) -contains 2 | True |
-notcontains | Checks if a collection does not contain a value. | @(1,2,3) -notcontains 4 | True |
-in | Checks if a value exists in a collection. | 5 -in @(2, 4, 5) | True |
-notin | Checks if a value does not exist in a collection. | 6 -notin @(2, 4, 5) | True |
-replace | Replaces text matching a regular expression pattern. | 'PowerShell' -replace 'Shell', 'Cmd' | PowerCmd |
Logical Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
-and | Returns true if both statements are true. | $true -and $true | True |
-or | Returns true if one of the statements is true. | $true -or $false | True |
-not | Reverses the logical state of its operand. | -not $false | True |
! | Alias for -not , reverses the logical state. | !$true | False |
-band | Performs a bitwise AND operation. | 5 -band 3 | 1 |
-bor | Performs a bitwise OR operation. | 5 -bor 3 | 7 |
-bxor | Performs a bitwise XOR operation. | 5 -bxor 3 | 6 |
Redirection Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
> | Redirects output to a file (overwrites existing content). | Get-Process > output.txt | Creates or overwrites output.txt with the process list. |
>> | Redirects output to a file (appends to existing content). | Get-Process >> output.txt | Appends process list to output.txt . |
2> | Redirects error output to a file (overwrites existing content). | Get-Process 2> errors.txt | Creates or overwrites errors.txt with error messages. |
2>> | Redirects error output to a file (appends to existing content). | Get-Process 2>> errors.txt | Appends error messages to errors.txt . |
2>&1 | Redirects error output to the same location as standard output. | Get-Process 2>&1 | Combines standard output and error messages. |
` | ` | Pipes the output of one command into another command. | `Get-Process |
Type Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
-is | Checks if an object is a specific type. | 'Hello' -is [string] | True |
-isnot | Checks if an object is not a specific type. | 'Hello' -isnot [int] | True |
-as | Attempts to convert an object to a specific type. | 5 -as [string] | "5" |
Range Operator
Operator | Description | Example Usage | Result |
---|---|---|---|
.. | Generates a sequence of numbers. | 1..5 | 1, 2, 3, 4, 5 |
Split and Join Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
-split | Splits a string into an array. | 'one,two,three' -split ',' | one , two , three |
-join | Joins an array into a string. | ('one', 'two', 'three') -join ',' | "one,two,three" |
Unary Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
+ | Unary plus, returns the value of the operand. | +5 | 5 |
- | Unary minus, negates the value of the operand. | -5 | -5 |
Special Operators
Operator | Description | Example Usage | Result |
---|---|---|---|
& | Runs a command, script, or script block. | & 'C:\script.ps1' | Runs script.ps1 . |
. (dot sourcing) | Runs a script in the current scope. | . .\script.ps1 | Executes script.ps1 in the current session, preserving any functions or variables. |
:: | Accesses static members of a class. | [System.Math]::Sqrt(25) | 5 (Square root of 25). |
? | Alias for Where-Object in pipelines. | Get-Process ? { $_.CPU -gt 100 } | Filters processes with CPU usage greater than 100. |
% | Alias for ForEach-Object in pipelines. | Get-Process % { $_.Name } | Outputs the names of processes. |