powershell - 哈希表数组上的格式表

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

最佳答案

使用 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

https://stackoverflow.com/questions/20874464/

相关文章:

android - 格式化日历日期

python - Python中的字符串格式化

python - 如何在 Python/Pandas 中将变量设置为 "Today' s"date

eclipse - 制表符和空格转换

php - 当我将我的数字指定为 .000021 时,为什么 PHP 会以科学计数法打印我的数字?

ruby - 如何将数字 1000 格式化为 "1 000"

html - 如何将 NUnit 输出转换为 HTML 报告

algorithm - 模糊日期算法

.net - 什么是 String.Format 的 WPF XAML 数据绑定(bind)等效项?

html - 每当我调整编辑器窗口的大小时,如何阻止代码行自动转移到下一行?