How to explode a Dictionary List as headers in a format-table

dictionary, formatting, powershell

Solution

Since Powershell v2 new-object has a `property` parameter which lets you enter a hash table in which the keys are the names of properties and the values are property value.

$tmp | select -expand data | %{new-object psobject -property $_}

gives:

header2                                                     header1
-------                                                     -------
Value2                                                      Value1
ValueB                                                      ValueA

Problem

UPDATED to include listing blank/empty properties from objects Sorry for the title, not sure how to label this question. I want to express a list of Dictionary objects with Key as Header/Property and Value as the header's/property's value. For example take the following PoSH code ``` $obj1 = new-object object | select Data; $obj1.Data = @{"header1"="Value1";"header2"="Value2";} $obj2 = new-object object | select Data; $obj2.Data = @{"header1"="ValueA";"header2"="ValueB";} $obj3 = new-object object | select Data; $obj3.Data = @{"header1"="Value1";"header3"="ValueC";} $tmp = @($obj1,$obj2,$obj3) ``` `$tmp` then looks like the following: ``` Data ---- {header2, header1} {header2, header1} {header3, header1} ``` `$tmp | select -Expand Data` gets the following useful information ``` Name Value ---- ----- header2 Value2 header1 Value1 header2 ValueB header1 ValueA header3 ValueC header1 Value1 ``` Anyway I can pivot the data and turn the Names into Properties (or headers) and express them with values i.e. ``` header1 header2 header3 ---- ----- ----- Value1 ValueB ValueA Value2 Value1 ValueC ``` Note: I've been able to do this by writing a function that takes each object in my Dictionary list, creates a new object and adds the Properties via Add-Member, but it's an expensive and slow process when you have thousands of entries and thousands of Dictionary Keys

Original source