Operators and Comparisons
Learn all PowerShell operators: arithmetic, comparison, logical, string matching, and redirection operators with practical examples.
📖 6 min read📅 2026-02-10Core Concepts
Arithmetic Operators
# Basic math
5 + 3 # 8 (Addition)
10 - 4 # 6 (Subtraction)
6 * 7 # 42 (Multiplication)
20 / 3 # 6.67 (Division)
20 % 3 # 2 (Modulus/remainder)
# Assignment operators
$x = 10
$x += 5 # $x is now 15
$x -= 3 # $x is now 12
$x *= 2 # $x is now 24
$x /= 4 # $x is now 6
$x %= 4 # $x is now 2
# Increment / Decrement
$i = 0
$i++ # $i is now 1
$i-- # $i is now 0
# Power
[math]::Pow(2, 10) # 1024
# Size constants
1KB # 1024
1MB # 1048576
1GB # 1073741824
1TB # 1099511627776
# Useful for file sizes
$size = 1.5GB
"$([math]::Round($size / 1MB, 2)) MB" # "1536 MB"Comparison Operators
PowerShell comparison operators are case-insensitive by default:
# Equality
5 -eq 5 # True (equal)
5 -ne 3 # True (not equal)
# Greater / Less than
10 -gt 5 # True (greater than)
10 -ge 10 # True (greater or equal)
3 -lt 7 # True (less than)
3 -le 3 # True (less or equal)
# String comparisons (case-insensitive by default)
"Hello" -eq "hello" # True
"Hello" -ceq "hello" # False (case-sensitive)
"Hello" -ieq "hello" # True (explicitly case-insensitive)Case-Sensitive Variants
Add c prefix for case-sensitive, i for explicit case-insensitive:
"ABC" -ceq "abc" # False
"ABC" -cne "abc" # True
"ABC" -clike "A*" # True
"ABC" -clike "a*" # FalseString Matching Operators
-like / -notlike (Wildcard)
"PowerShell" -like "Power*" # True
"PowerShell" -like "*Shell" # True
"PowerShell" -like "*wer*" # True
"PowerShell" -like "P??erShell" # True (? = single char)
"PowerShell" -notlike "Bash*" # True
# Filter arrays with wildcards
$names = "Alice", "Bob", "Alex", "Anna"
$names -like "A*" # @("Alice", "Alex", "Anna")-match / -notmatch (Regex)
# Basic regex matching
"PowerShell 7.4" -match "\d+\.\d+" # True
$Matches[0] # "7.4"
# Email validation
"[email protected]" -match "^[\w.-]+@[\w.-]+\.\w+$" # True
# Extract groups
"2026-02-10" -match "(\d{4})-(\d{2})-(\d{2})"
$Matches[1] # "2026"
$Matches[2] # "02"
$Matches[3] # "10"
# Named groups
"John is 30" -match "(?<name>\w+) is (?<age>\d+)"
$Matches.name # "John"
$Matches.age # "30"
# Array filtering
$emails = "[email protected]", "invalid", "[email protected]"
$emails -match "@" # @("[email protected]", "[email protected]")-replace (Regex Replace)
# Simple replace
"Hello World" -replace "World", "PowerShell"
# "Hello PowerShell"
# Regex replace
"Phone: 123-456-7890" -replace "\d", "X"
# "Phone: XXX-XXX-XXXX"
# Capture group replacement
"John Smith" -replace "(\w+) (\w+)", '$2, $1'
# "Smith, John"
# Remove pattern
"Hello World" -replace "\s+", " "
# "Hello World"-contains / -in (Collection)
# Check if collection contains value
$fruits = "Apple", "Banana", "Cherry"
$fruits -contains "Banana" # True
$fruits -contains "Grape" # False
$fruits -notcontains "Grape" # True
# Reverse syntax with -in
"Banana" -in $fruits # True
"Grape" -notin $fruits # TrueLogical Operators
# AND - both must be true
$true -and $true # True
$true -and $false # False
# OR - at least one must be true
$true -or $false # True
$false -or $false # False
# NOT - inverts the value
-not $true # False
-not $false # True
!$false # True (shorthand)
# XOR - exactly one must be true
$true -xor $false # True
$true -xor $true # False
# Practical examples
$age = 25
$hasLicense = $true
if ($age -ge 18 -and $hasLicense) {
Write-Host "Can drive"
}
$isWeekend = (Get-Date).DayOfWeek -in "Saturday", "Sunday"
if ($isWeekend -or $isHoliday) {
Write-Host "Day off!"
}Type Operators
# Type checking
42 -is [int] # True
"Hello" -is [string] # True
$null -is [object] # False
# Type conversion
42 -as [string] # "42"
"3.14" -as [double] # 3.14
"notanumber" -as [int] # $null (safe conversion)Range Operator
# Create a range
1..5 # @(1, 2, 3, 4, 5)
5..1 # @(5, 4, 3, 2, 1)
-3..3 # @(-3, -2, -1, 0, 1, 2, 3)
# Useful with ForEach
1..5 | ForEach-Object { "Item $_" }
# Generate alphabet
[char[]]([int][char]'A'..[int][char]'Z')Split and Join Operators
# Split
"one,two,three" -split "," # @("one", "two", "three")
"Hello World" -split "\s" # @("Hello", "World")
# Split with limit
"a-b-c-d" -split "-", 3 # @("a", "b", "c-d")
# Join
"Red", "Green", "Blue" -join ", " # "Red, Green, Blue"
1..5 -join "-" # "1-2-3-4-5"Ternary Operator (PowerShell 7+)
# Traditional if/else
$status = if ($age -ge 18) { "Adult" } else { "Minor" }
# Ternary operator (PowerShell 7+)
$status = $age -ge 18 ? "Adult" : "Minor"
# Null coalescing (PowerShell 7+)
$value = $null
$result = $value ?? "default" # "default"
# Null coalescing assignment
$value ??= "default" # Assigns "default" only if $value is $null
# Null conditional (Pipeline chain operators)
Get-Process -Name "notepad" -ErrorAction SilentlyContinue && Write-Host "Notepad is running"
Get-Process -Name "nonexistent" -ErrorAction SilentlyContinue || Write-Host "Process not found"Practical Examples
Example 1: Validate User Input
$email = Read-Host "Enter your email"
if ($email -match "^[\w.+-]+@[\w-]+\.[\w.]+$") {
Write-Host "Valid email: $email" -ForegroundColor Green
} else {
Write-Host "Invalid email format" -ForegroundColor Red
}Example 2: File Size Categorization
Get-ChildItem -File | ForEach-Object {
$sizeCategory = switch ($true) {
($_.Length -lt 1KB) { "Tiny" }
($_.Length -lt 1MB) { "Small" }
($_.Length -lt 100MB) { "Medium" }
($_.Length -lt 1GB) { "Large" }
default { "Huge" }
}
[PSCustomObject]@{
Name = $_.Name
Size = "$([math]::Round($_.Length / 1KB, 1)) KB"
Category = $sizeCategory
}
}Exercises
- Write an expression that checks if a number is between 10 and 100 (inclusive)
- Use
-matchto extract all IP addresses from a string - Use the range operator to generate even numbers from 2 to 20
- Replace all vowels in a string with asterisks using
-replace - Check if a file extension is in a list of allowed extensions using
-in
Next: Control Flow — learn about if/else, switch, loops, and more!