哈希表数组的格式表

时间:2014-01-01 23:43:33

标签: powershell formatting

如何使用Format-Table cmdlet格式化哈希表数组?

示例:

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

期望输出:

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

实际 输出

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

1 个答案:

答案 0 :(得分:46)

使用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解决方案:

$(foreach ($ht in $table)
 {new-object PSObject -Property $ht}) | Format-Table -AutoSize