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?