Updated March 18, 2023
What are PowerShell Operators?
We mostly use PowerShell Operators to execute commands. If you want to do something you will search for that command on the web and copy the command and paste it in either command prompt or PowerShell. But truly PowerShell is an object-oriented automation tool rather than a DOS Command-line interface.
PowerShell Operators
In this section, I will explain the commonly used operators supported by PowerShell. But It has a lot of operators. that I listed below,
- Arithmetic Operators (+, -, *, /, %)
- Assignment Operators (=, +=, -=, *=, /=, %=)
- Comparison Operators
- Equality Operators (-eq, -ne, -gt, -lt, -le, -ge)
- Matching Operators(-match, -notmatch, -replace)
- Containment comparison Operators (-in, -notin, -contains, -notcontains)
- Logical Operators (-and, -or, -xor, -not, !)
- Redirection Operators (>, >>, and >&1)
- Split and Join Operators (-split, -join)
- Type Operators (-is, -isnot, -as)
- Unary Operators (++, –)
- Special Operators
Different Types of PowerShell Operators
PowerShell Operators are easy to use. Here we will discuss how to use PowerShell Operators with the help of examples.
1. Arithmetic Operators
Arithmetic operators calculate numeric values. You can do addition, subtraction, multiplication, division, remainder operation. In addition + and * operates on string, arrays and hash tables.
Operator | Description | Example |
+ | Adds numeric values | 6 + 2 |
concatenates a string, arrays, and hash tables | “edu” + “CBA” | |
– | Subtracts numeric values | 8 – 9 |
Makes a number negative | -98 | |
* | Multiple numeric values | 6 * 2 |
copy string, arrays to the specified number of times | “!” * 3 | |
/ | Divides numeric values | 9 / 7 |
% | Gives remainder after division | 9 % 3 |
2. Assignment Operators
PowerShell Assignment operators assign, change or append values to variables.
Operator | Description | Example |
= | Assign value to variable | $a = 3 |
+= | Adds and assign value to the variable | $a += 4 |
Concatenates string at the end | $b = “Hello, “ $b += “World” |
|
Adds number to array | $a = 1,2,3 $a += 2 |
|
-= | Subtracts and assign value to the variable | $a -= 9 |
*= | Multiplies and assign value to the variable | $a *= 2 |
Appends string with a specified number of times | $e = “String “ $e * 5 |
|
/= | Divides and assign a value to the variable | $a /= 7 |
%= | Divides and assign a remainder value to the variable | $a %= 3 |
3. Comparison operators
- Equality Operators
Check for equality of values. This includes numeric, strings, array. It will return True or False are a result.
Operator | Description | Example |
-eq | Check for equal value | 1 -eq 1 |
Check for equal arrays | 1,2,3 -eq 2 | |
Check for equal strings | “Hello” -eq “World” | |
-ne | Check for non-equal value | 1 -ne 2 |
Check for non-equal arrays | 1,2,3 -ne 2 | |
Check for non-equal strings | “Hello” -ne “World” | |
-gt | Check for greater value | 8 -gt 6 |
Check all greater values in array and prints one by one | 7, 8, 9 -gt 8 | |
-ge | Check for greater or equal value | 8 -ge 8 |
Check all greater values or equal values in array and prints one by one | 7, 8, 9 -ge 8 | |
-lt | Check for lesser value | 8 -lt 6 |
Check all lesser values in array and prints one by one | 7, 8, 9 -lt 8 | |
-le | Check for lesser or equal value | 6 -le 8 |
Check all lesser values or equal values in array and prints one by one | 7, 8, 9 -le 8 |
- Matching Operators
These PowerShell operators are capable of finding elements with specific patterns using wild card expressions.
Operator | Description | Example |
-match | Matches a string with a specified regular expression | “Sunday”, “Monday”, “Tuesday” -match “sun” |
-notmatch | Does not match a string with a specified regular expression | “Sunday”, “Monday”, “Tuesday” -notmatch “sun” |
-replace | Check for the given string and replace with specified string | “book” -replace “B”, “C” |
- Containment Comparison Operators
These PowerShell Operators are used to checks for the existence of a specified element or array in an array.
Operator | Description | Example |
-contains | Checks for the existence of a specified element in an array | “red”, “yellow” -contains “red” |
-notcontains | Checks for the non-existence of specified element in an array | “red”, “yellow” -notcontains “green” |
-in | Checks for the existence of a specified element in an array | “red” -in “red”, “yellow” |
-notin | Checks for the non-existence of specified element in an array | “green” -notin “red”, “yellow” |
Note: both contain and in do the same operation, the operand order differs, in “contains” we take right-hand value to check against left-hand value. But in “in” we take left-hand value to check against right-hand value.
4. Logical Operators
It also allows us to use logical operations like AND, OR, NOT, XOR.
Operator | Description | Example |
-and | Truth with both statements is TRUE. | 1 -and 1 |
-or | Truth with any one of the statements is TRUE. | 1 -or 0 |
-xor | Truth when only of the statement is TRUE. | 1 -xor 0 |
-not | Negates the statement. | -not 1 |
! | Negates the statement | !0 |
5. Redirection Operator
These are used to redirect the output of one command as the input to another command.
Operator | Description | Example |
> | Send all success stream data to output | .\script.ps1 > script.log |
>> | Appends all success stream data to output | .\script.ps1 >> script.log |
n>&1 | Redirects a specified stream (n) to output | .\script.ps1 3>&1 script.log |
Note:
- 3>&1 – is for warning redirection
- 2>&1 – is for error redirection
6. Split and Join Operator
This PowerShell operator used to redirect the output of one command as the input to another command.’
Operator | Description | Example |
-split | Splits a string into to substring based on a delimiter | -split “one two three four” |
Splits string with a specified delimiter | “Lastname:FirstName:Address” -split “:” | |
-join | Joins given strings to a single string | -join “a”, “b”, “c” |
7. Type Operators
Type operators tell whether the given object is an instance of a specified .NET type.
Operator | Description | Example |
-is | Compares instance of the specified .NET type and return True if equal. | 32 -is “int” |
-isNot | Compares the instance of specified .NET and return False if not equal. | 32 -isNot “int” |
-as | Converts the given value to the specified type | “12/31/07” -as [DateTime] |
8. Unary Operators
These are the quick operators for increment and decrement in PowerShell. Mostly used on iteration.
Operator | Description | Example |
++ | Increments value by 1and assign back | $a = 9
$a++ |
— | Decrement value by 1 and assign back | $a = 9
$a– |
9. Special Operators
PowerShell Special operators have specific use cases that do not fit in any other group.
Operator | Description | Example |
@( ) | Display the result of one or more statements as a list. | @(Get-WmiObject win32_logicalDisk) |
& | Runs a command, script or block. | $c = “get-executionpolicy”
& $c |
[ ] | Converts object to specified type | [int64]$a = 34 |
, | Comma creates an array. | $myArray = 1,2,3 |
-f | Format operator, formats string by format methods | “{0} {1,-10} {2:N}” -f 1,”hello”,[math]::pi |
.. | Generates a range | ‘a’..’f’ |
Recommended Articles
This has been a guide to PowerShell Operators. Here we have discussed the basic concept along with various types of PowerShell Operators in detail. You can also go through our other suggested articles to learn more –