Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between pipeline ForEach-Object and -InputObject

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

ForEach-Object Documentation

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading