When we're trying to export data to other functions via the pipeline, we observe some strange behavior in PowerShell.
Example code:
$Array = @()
$Obj1 = [PSCustomObject]@{
Member1 = 'First'
Member2 = 'Second'
}
$Obj2 = [PSCustomObject]@{
Member1 = 'First'
Member2 = 'Second'
Member3 = 'Third'
}
$Array = $Obj1, $Obj2
$Array | Out-GridView -Title 'Not showing Member3'
$Array = $Obj2, $Obj1
$Array | Out-GridView -Title 'All members correctly displayed'
In the example above you can see that when the first object only contains 2 properties, the Out-GridView CmdLet (and others) only show 2 properties, even though the second object has 3 properties. However, when the first object in the array has 3 properties it does display them all correctly.
Is there a way around this? Because it's not possible to predict up front how many properties on an object there will be and if the object with the most properties will be the first one in the array.
I had the same experience once and created the following reusable '
Union' function:Usage:
It is also supposed to work with complex objects. Some standard cmdlets output multiple object types at once and if you view them (e.g.
Out-GridView) or dump them in a file (e.g.Export-Csv) you might miss a lot of properties. Take as another example:Added 2014-09-19:
Maybe this is already between the lines in the comments
$Array | Select * | …will not resolve the issue but specifically selecting the properties$Array | Select Member1, Member2, Member3 | …does.Besides, although in most cases the
Unionfunction will work, there are some exceptions to that as it will only align the first object with the rest. Consider the following object:If you
Unionthis object everything appears to be fine and if you e.g.ExportTo-CSVand work with theexport .csvfile from then on you will never have any issue.Still there is a catch as only the first object is aligned. If you e.g. sort the result on
Id(Sort Id) or take just the last 2 (Select -Last 2) entries, theNameis not listed because the second object doesn’t contain theNameproperty:Therefor I have rewritten the
Union-Object(AliasUnion) function`):Union-Object
Syntax:
Update 2021-08-25
Based on az1d helpful feedback on an error caused by equal property names with different casing, I have created a new
UnifyPropertiesfunction.(I will no longer use the name
for his)UnionObjectUsage:
See also:
#13906Add -UnifyProperties parameter to Select-Object