I assume this has something to do with how the data is passed via the pipeline, but take the two examples below;
$someData = Get-ChildItem
$obj = ForEach-Object -InputObject $someData -Process {[pscustomobject] @{Name=$_.Name; LastWriteTime = $_.LastWriteTime}}
$obj2 = Get-ChildItem | % {[pscustomobject] @{Name=$_.Name; LastWriteTime = $_.LastWriteTime}}
The object types differ, although I expected both to be of type [PSCustomObject]
, only one is;
$obj.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
$obj2.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
When $Obj2
is passed into a Format-Table
, I get the type of output I desire, i.e. an actual table of values. But $Obj
provides somewhat useless Format-Table
output. What's happening behind the scenes?
When you use the InputObject parameter with ForEach-Object, instead of piping command results to ForEach-Object, the InputObject value is treated as a single object.