Do Not Mix Different Objects!

by Feb 18, 2016

If you do output completely different objects, you may lose information. Take a look at this example:

#requires -Version 2

$hash = @{
Name = 'PowerShell Conference EU'
Date = 'April 20, 2016'
City = 'Hannover'
URL = 'www.psconf.eu'
}
New-Object -TypeName PSObject -Property $hash 

$b = Get-Process -Id $pid
$b

When you run the code, you get this:

  
Date           URL           Name                     City    
----           ---           ----                     ----    
April 20, 2016 www.psconf.eu PowerShell Conference EU Hannover
                             powershell_ise
 

It seems almost all properties from $b (process) were lost. The reason: PowerShell outputs objects in real time, and the first emitted object determines which properties are shown in the table. Any subsequent object will be fitted into this table.

If you must output different objects, pipe them to Out-Host. Each time you pipe to Out-Host, PowerShell creates a new output with new table headers.

Twitter This Tip! ReTweet this Tip!