0

Sample code below. Notice that $n in some ways acts like $null, but not when added to an array.

$n -eq $null, and $c -eq $n, yet they behave differently when added to an array.

How can you tell if the value of the variable is nothing vs. $null? (without adding it to an array)

How can you create a variable whose value is nothing? (Without using the get-nothing function?)

# $n is nothing .. it is defined, yet has no value, not even $null
function Get-Nothing
{
    write-host "Returns Nothing"
}

$n = Get-Nothing
$na = @($n)
Write-Host "N type is ($($n.gettype()))"                # "You cannot call a method on a null-valued expression."
Write-Host "NA is $($na.gettype()), count=$($na.count)" # count == 0
Write-host "N is Null? $($null -eq $n)"                 # true

# $c is $null.
$c = $null
$ca = @($c)
Write-Host "C type is ($($c.gettype()))"                # "You cannot call a method on a null-valued expression."
Write-Host "CA is $($ca.gettype()), count=$($ca.count)" # count == 1
Write-host "C is Null? $($null -eq $c)"                 # true

Write-Host "C == N ? $($c -eq $n)"                      # true

$a = 1,2,3
$ac = $a + $c
$an = $a + $n

Write-Host "AC count=$($ac.count)"  # Count == 4
Write-Host "AN count=$($an.count)"  # Count == 3
joeking
  • 2,006
  • 18
  • 29
  • 1
    I found this article, [everything-about-null](https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null?view=powershell-7.1) that also points this out. "$d" should have type **System.Management.Automation.Internal.AutomationNull**, but they don't answer my questions about it -- how to tell the difference between that and $null. – joeking Aug 24 '21 at 17:13
  • 1
    This answer explains it a lot better - in order to prevent conversion from `[AutomationNull]` to `$null`, use `ReferenceEquals` through reflection: https://stackoverflow.com/a/30018601/7411885 – Cpt.Whale Aug 24 '21 at 17:54
  • I'll note that `[AutomationNull]` 'empty' issues are specifically pipeline-related. Simply checking for and handling normal `$null` values earlier in your pipeline is almost always an option – Cpt.Whale Aug 24 '21 at 18:01
  • I got curious about this as I was looking into how to merge two arrays together. I often wrap return values with @($result). From this I know that @(Get-Nothing) will be a 0-length array - so I wondered if "$a + $b", where $a is an array, and $b is $null would effectively do nothing. It doesn't -- it makes a new array with the last element being $null. – joeking Aug 24 '21 at 22:30
  • Does this answer your question? [Why is an empty PowerShell pipeline not the same as null?](https://stackoverflow.com/questions/22343187/why-is-an-empty-powershell-pipeline-not-the-same-as-null) – iRon Aug 25 '21 at 07:11
  • `Get-Nothing`, or `@()`, is not just a 0-length array *but an array without any dimensions* where `$Null` type casted to an array `@($Null)` (based on the type of LHS of of the operation `$a + $b`, e.g. `@() + $Null`) is an `1` dimensional array with a length of `1` containing a `$Null`. – iRon Aug 25 '21 at 07:58

0 Answers0