如何使用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
答案 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