PowerShell Automatic Variables $null


Example

$null is used to represent absent or undefined value.
$null can be used as an empty placeholder for empty value in arrays:

PS C:\> $array = 1, "string", $null
PS C:\> $array.Count
3

When we use the same array as the source for ForEach-Object, it will process all three items (including $null):

PS C:\> $array | ForEach-Object {"Hello"}
Hello
Hello
Hello

Be careful! This means that ForEach-Object WILL process even $null all by itself:

PS C:\> $null | ForEach-Object {"Hello"} # THIS WILL DO ONE ITERATION !!!
Hello

Which is very unexpected result if you compare it to classic foreach loop:

PS C:\> foreach($i in $null) {"Hello"} # THIS WILL DO NO ITERATION
PS C:\>