Assignment operators store values in variables and, in their compound forms, combine an arithmetic or string operation with assignment. PowerShell uses the standard = for simple assignment. It also provides a family of compound operators (+=, -=, *=, /=, %=) for in-place updates. Additionally, PowerShell 7 introduced the null-coalescing assignment operator ??= to handle conditional defaults.
Operators at a Glance
| Operator | Name | Description | Example |
|---|---|---|---|
= |
Assignment | Assigns the right-hand value to the left-hand variable | $x = 10 |
+= |
Add and assign | Adds the right-hand value to the variable, then assigns the result | $x += 5 |
-= |
Subtract and assign | Subtracts the right-hand value from the variable | $x -= 3 |
*= |
Multiply and assign | Multiplies the variable by the right-hand value | $x *= 2 |
/= |
Divide and assign | Divides the variable by the right-hand value | $x /= 4 |
%= |
Modulus and assign | Computes the remainder and assigns it | $x %= 3 |
??= |
Null-coalescing assignment (PS 7+) | Assigns the right-hand value only if the variable is $null |
$x ??= 10 |
Basic Assignment (=)
The = operator assigns a value to a variable. PowerShell also supports multiple assignment, where several variables receive the same value in a single statement.
$name = "PowerShell"
$version = 7.4
$isStable = $true
$a = $b = $c = 0
[PSCustomObject]@{
Name = $name
Version = $version
IsStable = $isStable
A = $a
B = $b
C = $c
}pwsh ./basic-assignment.ps1Name : PowerShell
Version : 7.4
IsStable : True
A : 0
B : 0
C : 0Compound Assignment Operators
Compound operators perform an operation and assign the result back to the variable in one step. They work with numbers, strings, and arrays, depending on the operator.
+= (Add and Assign)
- Numbers: adds the right-hand value.
- Strings: concatenates the right-hand string.
- Arrays: appends elements to the array (creates a new array).
$count = 10
$count += 5
Write-Output "Count: $count"
$message = "Hello"
$message += ", World!"
Write-Output "Message: $message"
$list = @(1, 2, 3)
$list += 4
$list += 5
Write-Output "List: $($list -join ', ')"Count: 15
Message: Hello, World!
List: 1, 2, 3, 4, 5-=, *=, /=, %= (Numeric Operations)
These operators are used primarily with numeric values.
$x = 20
$x -= 5 # 20 - 5 = 15
$x *= 2 # 15 * 2 = 30
$x /= 3 # 30 / 3 = 10
$x %= 3 # 10 % 3 = 1
Write-Output "Final value: $x"Final value: 1Null-Coalescing Assignment (??=) (PowerShell 7+)
The ??= operator assigns the right-hand value to the left-hand variable only if the variable is $null. If the variable already has a non-null value, it remains unchanged. This is particularly useful for setting default values without overwriting existing configuration.
$config = $null
$config ??= "DefaultConfig"
Write-Output "Config: $config"
$config ??= "NewConfig"
Write-Output "Config: $config"Config: DefaultConfig
Config: DefaultConfigPractical Use: Default Parameters and Settings
$settings = @{}
$settings.Theme ??= "Light"
$settings.FontSize ??= 12
$settings.AutoSave ??= $true
$settingsName Value
---- -----
AutoSave True
Theme Light
FontSize 12Practical Examples
1. Counter and Accumulator
$total = 0
$items = 1, 2, 3, 4, 5
foreach ($item in $items) {
$total += $item
}
Write-Output "Total: $total"Total: 152. Building a String Report
$report = ""
$report += "=== System Report ===`n"
$report += "Hostname: $env:COMPUTERNAME`n"
$report += "User: $env:USERNAME`n"
$report += "Date: $(Get-Date -Format 'yyyy-MM-dd')`n"
Write-Output $report=== System Report ===
Hostname: MYPC
User: tomnolan
Date: 2026-09-233. Lazy Initialization with ??=
$cache = @{}
function Get-CachedData {
param([string]$Key, [scriptblock]$Factory)
$cache[$Key] ??= & $Factory
return $cache[$Key]
}
$result1 = Get-CachedData -Key "Expensive" -Factory { Start-Sleep -Seconds 1; "Computed" }
$result2 = Get-CachedData -Key "Expensive" -Factory { "Should not run" }
Write-Output "Result1: $result1"
Write-Output "Result2: $result2"Result1: Computed
Result2: ComputedBest Practices
- Use compound assignment (
+=,-=, etc.) for concise in-place updates. - Prefer
??=for setting defaults without overwriting existing values (PS 7+). - Avoid
+=on large arrays inside loops; useList<T>orArrayListfor performance. - Be mindful of type coercion: when you use
+=on a string with a number, the number will be converted to a string. - Use parentheses when mixing assignment with other operators to avoid ambiguity.
- Check PowerShell version before using
??=, as it is not available in Windows PowerShell 5.1.