HOWTO · PowerShell
Check for Null Values in PowerShell
Learn what PowerShell's $null automatic variable represents, how it behaves in expressions and collections, and which comparison or string check to use.
On this page
To test whether a PowerShell value is null, compare with $null on the left: $null -eq $value. Use -ne for the opposite check. This explicit comparison distinguishes null from values such as $false, 0, and an empty string, which can also behave as false in a conditional.
Introduction to Null Variable in PowerShell
$null is PowerShell’s automatic variable for a null value. It is a value you can assign, compare, or place in a collection; it does not mean that a string is empty or that every unassigned variable has an “unknown” value. In the usual mode, reading an unassigned variable produces $null; strict mode can change how that read is handled.
A command’s errors do not generally mean that it returned $null. Check the command’s documented result and error behavior when troubleshooting, then test the value you actually received. A null-sensitive operation can fail when it expects an object, collection, or string, so an explicit check before using the value can make the cause easier to locate.
Impacts of the Null Variable in PowerShell
The effect of $null depends on the expression and the value’s type. In an interpolated string, $null contributes no characters. Brackets in a string such as "Value: [$value]" make that position visible as []; they do not turn null into a different value.
Numeric expressions can coerce or propagate null depending on the operator and context. Do not assume one numeric result for every expression: check the relevant operation and validate inputs before calculations.
Collections need separate care. Indexing a null collection is different from asking an existing array for an index outside its range. The first operation can produce an error; the latter commonly yields $null in ordinary PowerShell use. If the distinction matters to a script, check the collection itself and its bounds before indexing.
For objects, reading a missing property commonly yields $null outside strict mode. A property that exists but contains null has the same value to a simple null comparison, so check the object’s shape separately when that distinction matters. Calling a method on $null fails because there is no object to receive the call. When diagnosing such a failure, inspect the value immediately before the method call; exact error wording can vary by PowerShell version and context.
Checking for Null Values in PowerShell
Use Comparison Operators -eq and -ne to Check for Null Values in PowerShell
Put $null on the left of -eq or -ne when testing one value. This avoids a collection comparison changing the result: when an array is on the left, -eq and -ne can return matching array elements rather than a single Boolean. An array containing nulls can therefore behave differently in an if condition than a scalar value. With $null on the left, the comparison asks whether the value itself is null.
Run this example:
$value = 'ready'
if ($null -eq $value) {
Write-Output 'Value is null.'
} else {
Write-Output 'Value is present.'
}
Use the -not Operator to Check for Null Values in PowerShell
-not tests whether an expression is falsey, not whether it is specifically $null. It can also match $false, numeric zero, an empty string, or an empty collection. Use it when that broader condition is what you want; use $null -eq $value when the question is specifically whether a value is null.
Use the IsNullOrEmpty() Method to Check for Null Values in PowerShell
For a string, [string]::IsNullOrEmpty($text) is a .NET string method that returns true when the argument is null or has length zero. It does not match a string containing spaces. Use this check when either a missing string or an empty string should count as no text; it is not a general test for arbitrary PowerShell values.
Use the IsNullOrWhiteSpace() Method to Check for Null Values in PowerShell
[string]::IsNullOrWhiteSpace($text) matches null, an empty string, and a string made only of whitespace characters. Choose it when whitespace-only input should also count as blank. For example, assigning $text = ' ' before calling the method tests whitespace-only input; assigning $null tests its null behavior instead.
Conclusion
Use $null -eq $value for a direct null check and $null -ne $value when the value must be present. Choose -not only when you intend to test truthiness, and choose the string methods according to whether empty or whitespace-only text should count as blank. Checking the value before indexing it or calling a method helps distinguish a null input from other failures.