PowerShell Operators
Operators are symbols or keywords that perform specific actions on values. PowerShell supports arithmetic, assignment, comparison, logical, string, and type operators. Understanding operators is essential for writing conditions, calculations, and filtering logic in scripts.
Types of Operators
PowerShell Operators
|
+----|----+-----------+-----------+-----------+----------+
| | | | | |
Arithmetic Assignment Comparison Logical String Type
(+,-,*,/) (=,+=,-=) (-eq,-ne) (-and,-or) (-like) (-is,-as)
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333… |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ++ | Increment by 1 | $x++ | $x + 1 |
| -- | Decrement by 1 | $x-- | $x - 1 |
$a = 20
$b = 6
Write-Host "Add: $($a + $b)" # 26
Write-Host "Subtract: $($a - $b)" # 14
Write-Host "Multiply: $($a * $b)" # 120
Write-Host "Divide: $($a / $b)" # 3.33...
Write-Host "Modulus: $($a % $b)" # 2
$count = 5
$count++
Write-Host "After ++: $count" # 6
$count--
Write-Host "After --: $count" # 5
Assignment Operators
Assignment operators store values in variables and update them.
| Operator | Meaning | Equivalent To |
|---|---|---|
| = | Assign | $x = 10 |
| += | Add and assign | $x = $x + 5 |
| -= | Subtract and assign | $x = $x - 5 |
| *= | Multiply and assign | $x = $x * 5 |
| /= | Divide and assign | $x = $x / 5 |
| %= | Modulus and assign | $x = $x % 5 |
$score = 100
$score += 20 # score is now 120
Write-Host $score
$score -= 15 # score is now 105
Write-Host $score
$score *= 2 # score is now 210
Write-Host $score
$score /= 3 # score is now 70
Write-Host $score
Comparison Operators
Comparison operators compare two values and return True or False. PowerShell uses keyword-based operators instead of symbols like == or !=.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| -eq | Equal to | 5 -eq 5 | True |
| -ne | Not equal to | 5 -ne 3 | True |
| -gt | Greater than | 10 -gt 5 | True |
| -lt | Less than | 3 -lt 8 | True |
| -ge | Greater than or equal to | 5 -ge 5 | True |
| -le | Less than or equal to | 4 -le 5 | True |
$marks = 75
Write-Host ($marks -eq 75) # True
Write-Host ($marks -ne 80) # True
Write-Host ($marks -gt 60) # True
Write-Host ($marks -lt 50) # False
Write-Host ($marks -ge 75) # True
Write-Host ($marks -le 74) # False
Case-Insensitive and Case-Sensitive Comparisons
# Default: case-insensitive
"hello" -eq "HELLO" # True
# Force case-sensitive with 'c' prefix
"hello" -ceq "HELLO" # False
"hello" -ceq "hello" # True
# Force case-insensitive with 'i' prefix (same as default)
"hello" -ieq "HELLO" # True
Logical Operators
Logical operators combine multiple conditions. They are used most often inside if statements and filters.
| Operator | Meaning | Returns True When |
|---|---|---|
| -and | Logical AND | Both conditions are True |
| -or | Logical OR | At least one condition is True |
| -not | Logical NOT | The condition is False |
| ! | Logical NOT (short form) | The condition is False |
| -xor | Exclusive OR | Exactly one condition is True |
$age = 25
$hasID = $true
# Both must be true
if ($age -ge 18 -and $hasID) {
Write-Host "Entry allowed"
}
# At least one must be true
$isAdmin = $false
$isManager = $true
if ($isAdmin -or $isManager) {
Write-Host "Access granted"
}
# Reverse a condition
$isDenied = $false
if (-not $isDenied) {
Write-Host "Proceed"
}
Output:
Entry allowed
Access granted
Proceed
String Operators
String operators compare and search within text values.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| -like | Wildcard match | "PowerShell" -like "Power*" | True |
| -notlike | Wildcard non-match | "Python" -notlike "Power*" | True |
| -match | Regex match | "hello123" -match "\d+" | True |
| -notmatch | Regex non-match | "hello" -notmatch "\d+" | True |
| -contains | Array contains value | @(1,2,3) -contains 2 | True |
| -notcontains | Array does not contain | @(1,2,3) -notcontains 5 | True |
| -in | Value in array | 2 -in @(1,2,3) | True |
| -notin | Value not in array | 5 -notin @(1,2,3) | True |
# Wildcard matching with -like
$filename = "report_2026.xlsx"
$filename -like "report_*" # True
$filename -like "*.xlsx" # True
$filename -like "*.pdf" # False
# Match using regex
$email = "user@example.com"
$email -match "@" # True
$email -match "^\w+@\w+\.\w+$" # True (simple email pattern)
# Contains check in array
$fruits = @("apple", "banana", "mango")
$fruits -contains "banana" # True
$fruits -contains "grape" # False
Type Operators
Type operators check or convert the type of an object.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| -is | Check if object is a type | "text" -is [string] | True |
| -isnot | Check if not a type | 42 -isnot [string] | True |
| -as | Convert to a type (no error) | "42" -as [int] | 42 |
$value = 99
$value -is [int] # True
$value -is [string] # False
$value -isnot [string] # True
# Safe conversion with -as (returns null if failed, no error)
$converted = "123" -as [int]
Write-Host $converted # 123
$failed = "abc" -as [int]
Write-Host $failed # (blank – returns null, no error)
Range Operator
The range operator (..) generates a sequence of integers.
# Generate a range from 1 to 5
1..5 # Output: 1 2 3 4 5
# Use in a loop
foreach ($n in 1..5) {
Write-Host "Number: $n"
}
# Reverse range
5..1 # Output: 5 4 3 2 1
Operator Precedence
When multiple operators appear in one expression, PowerShell follows a specific order of evaluation (highest priority first):
- Parentheses
( ) - Unary operators (
-not,!,-) - Multiplication, Division, Modulus (
*,/,%) - Addition, Subtraction (
+,-) - Comparison operators (
-eq,-gt, etc.) - Logical AND (
-and) - Logical OR (
-or)
# Without parentheses
$result = 2 + 3 * 4 # 14 (multiplication first)
# With parentheses
$result = (2 + 3) * 4 # 20 (addition first)
Write-Host $result # 20
Summary
PowerShell operators cover every kind of comparison, calculation, and filtering task. Arithmetic operators do math. Comparison operators return True or False. Logical operators combine conditions. String operators search and match text patterns. Type operators check and convert data types. Using the right operator in the right context keeps scripts clean, efficient, and error-free.
