2

I'm revising code written by another author. While breaking up long code blocks to functions I found this behavior.

This code generates no error and the 2nd line appears to produce a $null return value.

PS C:\> $foo = $bar | Where-Object {$bar -eq "''"}
PS C:\> $foo | Get-Content

If I try to take the results of the first line and send it to a function as a parameter I get an expected error similar to the following.

PS C:\> $foo = $null
PS C:\> $foo | Get-Content
Get-Content : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:8
+ $foo | Get-Content
+        ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetContentCommand

From a programming view it appears that the old code was "working by accident".

What is is actually being returned by Where-Object in the first example if it's not $null?

pscitpro
  • 21
  • 2
  • 4
    See also: https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null?view=powershell-7.2#empty-null – zett42 Oct 08 '22 at 15:45
  • 1
    In short, the process block of a PoweShell cmdlet does not run on AutomationNull.Value. hopefully the linked duplicate gives a more in depth explanation – Santiago Squarzon Oct 08 '22 at 15:50

0 Answers0