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 = ''
New-Object -TypeName PSObject -Property $hash 

$b = Get-Process -Id $pid

When you run the code, you get this:

Date           URL           Name                     City    
----           ---           ----                     ----    
April 20, 2016 PowerShell Conference EU Hannover

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!