Search notes:

PowerShell: The automatic variable $NULL

$null is an automatic variable. It is the only instance of the null type.
$null is also referred to as null value.
The value of a PowerShell variable that was not assigned a value (yet) is $null. The value of $null itself is the C# value null.
$null can be assigned any value; yet, the value of $null remains unchanged:
PS C:\> $null = mkdir temp-dir
PS C:\> $null
Assigning a value of an expression to $null is typically used to hide the returned result on the console. In order to be more explicit about this intention, some prefer to write mkdir temp-dir | out-null instead.
$null has a count property whose value is 0:
PS C:\> $null.count
0

Types that can't be null

Strings cannot be $null even though $null can be assigned to them.
PS C:\> [string] $txt = $null
PS C:\> $txt -eq $null
False
A string that was assigned $null is the empty string:
PS C:\> $txt -eq ''
True
Integers cannot be $null as well and become 0 if a $null is assigned to them:
PS C:\> [int] $i = $null
PS C:\> $i -eq $null
False
PS C:\> $i
0

Checking for a null value

Unlike the null value in SQL, $null can be compared to itself. Thus, comparison operators such as -eq can be used to check if a variable is $null:
if ($val -eq $null) {
  'val is null'
} else {
  'val is not null'
}

See also

$null is implemented by the System.Management.Automation.NullVariable class.
Other automatic variables
The ValidateNotNull(), allowNull() and validateNotNullOrEmpty() attributes for parameters.

Index