PowerShell Null Handling: $null, -eq $null, and Null Coalescing

A null reference crash at 3 AM because someone’s display name wasn’t populated in Active Directory — that’s preventable. PowerShell null handling comes down to understanding what $null is, where it comes from, and how to defend against it. PowerShell 7 also adds the null coalescing (??) and null conditional (?.) operators that make safe null handling significantly more concise. This post covers every pattern you need.
What Is $null in PowerShell
$null is a special automatic variable representing the absence of a value. It is a singleton — there is only one $null. Uninitialized variables, function returns that produce no output, and properties that don’t exist all evaluate to $null.
# Uninitialized variable is $null
$x
$x -eq $null # True
# Function with no output returns $null
function Get-Nothing { }
$result = Get-Nothing
$result -eq $null # True
# Missing property returns $null
$obj = [PSCustomObject]@{ Name = "test" }
$obj.NonExistentProperty -eq $null # True
Checking for Null with -eq $null
The idiomatic way to check if something is null in PowerShell is -eq $null. Use it in if conditions to guard against null before using a value.
$value = Get-ADUser -Identity "jsmith" -Properties Manager -ErrorAction SilentlyContinue
if ($value -eq $null) {
Write-Warning "User not found"
} else {
Write-Output "Found user: $($value.Name)"
}
# Also check before accessing nested properties
if ($value.Manager -ne $null) {
$managerName = (Get-ADUser $value.Manager).Name
Write-Output "Manager: $managerName"
}
Null on the Left Side of -eq
This is an important pattern: put $null on the left side of the comparison when testing a value that might be an array. When a collection is on the right side of -eq $null, PowerShell returns the null elements rather than a Boolean.
# Value is an array — testing right-side $null returns array elements not Boolean
$values = @(1, $null, 3, $null)
$values -eq $null # Returns: $null, $null (the two nulls — not $true/$false!)
# CORRECT: put $null on the left
$null -eq $values # Returns: $false (correct Boolean)
# For scalars, both work the same
$x = $null
$x -eq $null # True — fine for scalars
$null -eq $x # True — defensive pattern, safe for both
The safest habit: always put $null on the left side.
Null Coalescing with ?? (PS7)
PowerShell 7 adds the ?? null coalescing operator. If the left side is $null, it returns the right side. This replaces verbose if ($x -eq $null) { $x = "default" } patterns.
# Null coalescing operator (PowerShell 7+)
$name = $null
$displayName = $name ?? "Unknown User"
Write-Output $displayName # Unknown User
# Chain multiple defaults
$a = $null
$b = $null
$c = "fallback"
$result = $a ?? $b ?? $c # Returns "fallback"
# Useful for optional config values
$timeout = $config.Timeout ?? 30 # Use config value or default to 30
Null Conditional Operator ?. (PS7)
The ?. null conditional operator (also called safe navigation) accesses a property or calls a method only if the object is not null. If it is null, the expression returns $null instead of throwing an error.
# Safe navigation — no error if $user is null
$user = Get-ADUser -Identity "missinguser" -ErrorAction SilentlyContinue
$displayName = $user?.DisplayName
# Without safe navigation (older PowerShell / PS5.1)
$displayName = if ($null -ne $user) { $user.DisplayName } else { $null }
# Chain safe navigation
$city = $data?.Address?.City # Returns $null if $data or Address is null
# Null conditional with method call
$length = $text?.Length # Returns $null if $text is $null, Length if not
Default Values for Null Parameters
In functions, provide default values for parameters that callers might not supply. Use the assignment shorthand or null coalescing to set defaults inside the function body.
function Send-Report {
param(
[string]$Recipient,
[string]$Subject = "Automated Report", # Default parameter value
[int]$RetryCount = 3 # Default parameter value
)
# Null coalescing for values that need runtime defaults
$from = $env:REPORT_SENDER ?? "[email protected]"
$logPath = $env:LOG_PATH ?? "C:\Logs\reports"
Write-Output "Sending to $Recipient, from $from, log at $logPath"
}
Send-Report -Recipient "[email protected]"
Common Errors and Fixes
- $null -eq $var vs $var -eq $null with arrays: When
$varis an array,$var -eq $nulldoes not return a Boolean — it returns the array elements that equal null. This silently breaks Boolean logic. Always write$null -eq $var(null on the left) for reliable Boolean null checks that work for both scalars and arrays. - String ‘null’ vs actual $null are different: The string
"null"is not$null. A string with the value"null"is truthy, non-empty, and does not equal$null. This often appears when API responses or CSV data contain the literal text"null"instead of an absent value. Check for both:if ($null -eq $value -or $value -eq "null").
Related Cmdlets / See Also
Wrapping Up
Defensive null handling — checking before accessing, defaulting with ??, and using ?. for safe navigation — prevents the most common class of runtime errors in production scripts. As a next step, enable strict mode in your scripts with Set-StrictMode -Version Latest — it turns undefined variable access into terminating errors, forcing you to handle null cases explicitly rather than silently proceeding with unexpected values.


