Powershell: Why is ,(1)[1] different from ,$null?

Created on 10 Mar 2018  ·  3Comments  ·  Source: PowerShell/PowerShell

I'm trying to understand why the following two statements yield different outputs:

&{,$null} | % {'x'} # x
&{,(1)[1]} | % {'x'} # no output

,$null and ,(1)[1] both seem to be arrays containing a single null element. Yet they behave differently during output from a scriptblock. What is happening here?

Issue-Question Resolution-Answered

All 3 comments

The second returns AutomationNull.Value which is not enumerated by the pipeline.

$null is enumerated.

See for example the implementation of foreach:
https://github.com/PowerShell/PowerShell/blob/bcf86d4f8a1ce0ef11f0e9100490e790fb067574/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs#L2748

Thank you @powercode. For other readers, here are some resources on AutomationNull:

@alx9r: This comes down to the question you later asked in #6436 and which I've brought into focus in https://github.com/PowerShell/PowerShell/issues/6436#issuecomment-375151317, but I've decided to create a separate issue for it: #6823

Was this page helpful?
0 / 5 - 0 ratings