Skip to content

PowerShell Assignment Operators

How to use PowerShell's basic (=), compound (+=, -=, *=, /=, %=), and null-coalescing (??=) assignment operators for efficient variable manipulation and default value handling.

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.

basic-assignment.ps1powershell
$name = "PowerShell"
$version = 7.4
$isStable = $true

$a = $b = $c = 0

[PSCustomObject]@{
    Name     = $name
    Version  = $version
    IsStable = $isStable
    A        = $a
    B        = $b
    C        = $c
}
Consolepowershell
pwsh ./basic-assignment.ps1
Outputplaintext
Name     : PowerShell
Version  : 7.4
IsStable : True
A        : 0
B        : 0
C        : 0

Compound 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).
add-assign.ps1powershell
$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 ', ')"
Outputplaintext
Count: 15
Message: Hello, World!
List: 1, 2, 3, 4, 5

-=, *=, /=, %= (Numeric Operations)

These operators are used primarily with numeric values.

numeric-compound.ps1powershell
$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"
Outputplaintext
Final value: 1

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

null-coalescing.ps1powershell
$config = $null
$config ??= "DefaultConfig"
Write-Output "Config: $config"

$config ??= "NewConfig"
Write-Output "Config: $config"
Outputplaintext
Config: DefaultConfig
Config: DefaultConfig

Practical Use: Default Parameters and Settings

default-settings.ps1powershell
$settings = @{}


$settings.Theme ??= "Light"
$settings.FontSize ??= 12
$settings.AutoSave ??= $true

$settings
Outputplaintext
Name                           Value
----                           -----
AutoSave                       True
Theme                          Light
FontSize                       12

Practical Examples

1. Counter and Accumulator

counter.ps1powershell
$total = 0
$items = 1, 2, 3, 4, 5

foreach ($item in $items) {
    $total += $item
}

Write-Output "Total: $total"
Outputplaintext
Total: 15

2. Building a String Report

string-builder.ps1powershell
$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
Outputplaintext
=== System Report ===
Hostname: MYPC
User: tomnolan
Date: 2026-09-23

3. Lazy Initialization with ??=

lazy-init.ps1powershell
$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"
Outputplaintext
Result1: Computed
Result2: Computed

Best 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; use List<T> or ArrayList for 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.
Navigation