1

This happens with Powershell version 5.1.19041.3031.

I found something very weird that I cannot explain:

$x = $null
'x is null: {0}' -f ($null -eq $x)
'array containing x has size {0}' -f @($x).Count

$y = $null | Select-Object
'y is null: {0}' -f ($null -eq $y)
'array containing y has size {0}' -f @($y).Count

This outputs the following:

x is null: True
array containing x has size 1
y is null: True
array containing y has size 0

It looks like $x and $y are both $null but a different one. On both I cannot call .GetType() yet they behave differently.


The real problem I was trying to solve is this:

$x = (Get-Bla).Name
Compare-Object @($x) @() # moans about $x being null sometimes when Get-Bla returns nothing

If I use $x = (Get-Bla).Name | Select-Object it fixes the problem but I couldn't understand the difference Select-Object was doing in this case.

Silex
  • 1,707
  • 17
  • 24
  • 3
    See: [`Empty Null`](https://learn.microsoft.com/powershell/scripting/learn/deep-dives/everything-about-null#empty-null), does this answer your question: [Why is piping null to set-variable causing different outcomes?](https://stackoverflow.com/a/76631985/1701026)? – iRon Aug 23 '23 at 11:20
  • X and Y processing is different. Y you are using Select-Object. If something is null why are you testing the length of a null object? – jdweng Aug 23 '23 at 12:30
  • To summarize: In addition to `$null`, PowerShell has a "null enumerable" aka "Automation Null" (the [`[System.Management.Automation.Internal.AutomationNull]::Value`](https://docs.microsoft.com/en-US/dotnet/api/System.Management.Automation.Internal.AutomationNull.Value) singleton), which represent the _absence of output_ from a command. _In expressions_, it behaves the same as `$null`, but it acts differently _in a pipeline_, where it acts like an _empty collection_ and therefore sends nothing through it. – mklement0 Aug 23 '23 at 13:18

0 Answers0