Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format-Table on Array of Hash Tables

How do you format an array of hash tables with the Format-Table cmdlet?

Example:

$table = @( @{ColumnA="Able";    ColumnB=1},             @{ColumnA="Baker";   ColumnB=2},             @{ColumnA="Charlie"; ColumnB=3} ) $table | Format-Table 

Desired Output:

ColumnA                        ColumnB ----                           ----- Able                           1 Baker                          2 Charlie                        3 

Actual Output:

Name                           Value ----                           ----- ColumnA                        Able ColumnB                        1 ColumnA                        Baker ColumnB                        2 ColumnA                        Charlie ColumnB                        3 
like image 944
Joseph Sturtevant Avatar asked Jan 01 '14 23:01

Joseph Sturtevant


1 Answers

Using Powershell V4:

$table = @( @{ColumnA="Able";    ColumnB=1},             @{ColumnA="Baker";   ColumnB=2},             @{ColumnA="Charlie"; ColumnB=3} )  $table | ForEach {[PSCustomObject]$_} | Format-Table -AutoSize   ColumnA ColumnB ------- ------- Able          1 Baker         2 Charlie       3 

V2 solution:

$(foreach ($ht in $table)  {new-object PSObject -Property $ht}) | Format-Table -AutoSize 
like image 88
mjolinor Avatar answered Sep 21 '22 11:09

mjolinor