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.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.333…
%Modulus (remainder)10 % 31
++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.

OperatorMeaningEquivalent 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 !=.

OperatorMeaningExampleResult
-eqEqual to5 -eq 5True
-neNot equal to5 -ne 3True
-gtGreater than10 -gt 5True
-ltLess than3 -lt 8True
-geGreater than or equal to5 -ge 5True
-leLess than or equal to4 -le 5True

$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.

OperatorMeaningReturns True When
-andLogical ANDBoth conditions are True
-orLogical ORAt least one condition is True
-notLogical NOTThe condition is False
!Logical NOT (short form)The condition is False
-xorExclusive ORExactly 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.

OperatorMeaningExampleResult
-likeWildcard match"PowerShell" -like "Power*"True
-notlikeWildcard non-match"Python" -notlike "Power*"True
-matchRegex match"hello123" -match "\d+"True
-notmatchRegex non-match"hello" -notmatch "\d+"True
-containsArray contains value@(1,2,3) -contains 2True
-notcontainsArray does not contain@(1,2,3) -notcontains 5True
-inValue in array2 -in @(1,2,3)True
-notinValue not in array5 -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.

OperatorMeaningExampleResult
-isCheck if object is a type"text" -is [string]True
-isnotCheck if not a type42 -isnot [string]True
-asConvert 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):

  1. Parentheses ( )
  2. Unary operators (-not, !, -)
  3. Multiplication, Division, Modulus (*, /, %)
  4. Addition, Subtraction (+, -)
  5. Comparison operators (-eq, -gt, etc.)
  6. Logical AND (-and)
  7. 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.

Leave a Comment