PowerShell中Format-Table命令的最大列数是多少?

6
我正在编写一个脚本,使用Format-Table以列的形式输出内容,但最多只能显示9列(带有或不带有-AutoSize参数都是如此)。
以防万一这不是我的问题,而是某些未记录的问题: Format-Table能够显示的最大列数是多少?
如果没有限制,我将从我的脚本中提取一个小的可重现案例。
我正在使用PowerShell 4.0,并像这样创建要列出的对象:
    New-Object PSCustomObject -Property ([Ordered] @{
        "BDS #" = $bdsVersion
        "HKCU" = $hkcuBasePath
        "HKLM" = $hklmBasePath
        "Name" = $fullName
        "CompilerVersion" = $compilerVersion
        "RTLVersion" = $rtlVersion
        "Define" = $define
        "Characterset" = $characterSet
        "Architectures" = $architecture
        "Frameworks" = $framework
        "DllSuffix" = $dllSuffix
    })
< p >尽管我的控制台窗口足够宽(300个字符宽),但 DllSuffix列仍未显示。

编辑

根据{{link1:建议}} {{link2:Cole9350} }的建议,我添加了{{link3:-Wrap-AutoSize 参数}}以格式化表格,但仍然失败:

function Get-BDS-ProductSummaries {
    $bdsVersions = Get-BDS-Versions
    $bdsVersions | ForEach-Object { 
        $summary = Get-BDS-ProductSummary $_
        $summary
    } | Format-Table -AutoSize -Wrap
    # http://blogs.technet.com/b/nexthop/archive/2011/03/21/psformatorselect.aspx
    Write-Host "Slash separated values like xxx/yyy means native/.NET"
}

我将从脚本的“主”部分调用此函数:
$args | ForEach-Object {
    $arg = $_
    switch ($arg) {
# ...
        'ProductSummaries' {
            Write-Host "Product Summaries:"
            Get-BDS-ProductSummaries
        }
# ...
    }
    $anyArg = $True
}

链接介绍如何将-Wrap-Format-Table结合使用,还指出有时不会显示所有列,但未说明在什么情况下不会显示所有列。
因此,我正在寻求指导:何时不会显示它们全部?
我甚至重新排列了一些字段,并将HKCU / HKLM路径合并为一个字段,并将其放在最后。
    New-Object PSCustomObject -Property ([Ordered] @{
        "BDS #" = $bdsVersion
        "Name" = $fullName
        "CompilerVersion" = $compilerVersion
        "RTLVersion" = $rtlVersion
        "Define" = $define
        "Characterset" = $characterSet
        "Architectures" = $architecture
        "Frameworks" = $framework
        "DllSuffix" = $dllSuffix
        "ProjectVersion" = $projectVersion
    #    "HKCU" = $hkcuBasePath
    #    "HKLM" = $hklmBasePath
        "HKCU/HKLM registry path" = $basePath
    })

它显示了一个165字符宽的表格(而我的控制台窗口是300个字符宽):
Product Summaries:

BDS # Name                    CompilerVersion RTLVersion Define               Characterset Architectures                         Frameworks DllSuffix ProjectVersion
----- ----                    --------------- ---------- ------               ------------ -------------                         ---------- --------- --------------
    1 Borland C# Builder 1    C#              C#         C#                   Unicode      C# .NET 1                             .NET       ????      ????
    2 Borland Delphi 8        none/16.0       none/16.0  VER160/VER160        Ansi/Unicode Win32;.NET 1                          VCL/.NET   80        80
    3 Borland Delphi 2005     17.0/17.0       17.0/17.0  VER170/VER170        Ansi/Unicode Win32;.NET 1                          VCL/.NET   90        ????
    4 Borland Delphi 2006     18.0/18.0       18.0/18.0  VER180/VER180        Ansi/Unicode Win32;.NET 2                          VCL/.NET   100       ????
    5 Borland Delphi 2007     18.5/19.0       18.0/19.0  VER180&VER185/VER190 Ansi/Unicode Win32;.NET 2                          VCL/.NET   100       ????
    6 CodeGear Delphi 2009    20.0            20.0       VER200               Unicode      Win32                                 VCL        120       11.1;12.0
    7 CodeGear Delphi 2010    21.0            21.0       VER210               Unicode      Win32                                 VCL        140       12.0
    8 Embarcadero Delphi XE   22.0            22.0       VER220               Unicode      Win32                                 VCL        150       12.2;12.3
    9 Embarcadero Delphi XE2  23.0            23.0       VER230               Unicode      Win32;Win64                           VCL        160       13.4
   10 Embarcadero Delphi XE3  24.0            24.0       VER240               Unicode      Win32;Win64;OSX32                     VCL;FMX1   170       14.3;14.4
   11 Embarcadero Delphi XE4  25.0            25.0       VER250               Unicode      Win32;Win64;OSX32;iOS-Arm             VCL;FMX2   180       14.6
   12 Embarcadero Delphi XE5  26.0            26.0       VER260               Unicode      Win32;Win64;OSX32;iOS-Arm;Android-Arm VCL;FMX2   190       15.1
   13 Embarcadero Appmethod 1 ????            ????       ????                 Unicode      ????                                  FMX2       ????      ????
   14 Embarcadero Delphi XE6  27.0            27.0       VER270               Unicode      Win32;Win64;OSX32;iOS-Arm;Android-Arm VCL;FMX2   200       15.4
   15 Embarcadero Appmethod 2 ????            ????       ????                 Unicode      ????                                  FMX2       ????      ????

路径更靠近开头时,输出宽度为181个字符:
BDS # HKCU/HKLM registry path        Name                    CompilerVersion RTLVersion Define               Characterset Architectures                         Frameworks DllSuffix
----- -----------------------        ----                    --------------- ---------- ------               ------------ -------------                         ---------- ---------

使用-Wrap,但不使用-AutoSize,最后一列也无法显示。
当省略Format-Table时,它会显示所有字段,因此New-Object PSCustomObject -Property确实会生成所有属性:
BDS #                   : 14
HKCU/HKLM registry path : \Software\Embarcadero\BDS\14.0
Name                    : Embarcadero Delphi XE6
CompilerVersion         : 27.0
RTLVersion              : 27.0
Define                  : VER270
Characterset            : Unicode
Architectures           : Win32;Win64;OSX32;iOS-Arm;Android-Arm
Frameworks              : VCL;FMX2
DllSuffix               : 200
ProjectVersion          : 15.4

2
这不是固定的列数,而是取决于每个列中值的最大大小。只需使用“-wrap”参数或“Select -expandProperty”即可... 无论哪种方式,这只是用于控制台输出。 - Cole9350
@Cole9350,你应该将你的评论转换为问题的答案。 - Keith Hill
@Cole9350 我试过了,不起作用。我刚刚编辑了我的答案,说明了我尝试过的内容。 - Jeroen Wiert Pluimers
3个回答

13

默认情况下,Format-Table只会显示10列。要全部显示,请使用“*”。有关详细信息,请参见下面的示例和输出。

(FYI:当列正在显示但其中的数据被截断时,使用-Wrap。)

示例:

$aryTemp = @()
$objTemp = New-Object PSObject
$objTemp | Add-Member -type NoteProperty -Name Column1 -Value "Data1"
$objTemp | Add-Member -type NoteProperty -Name Column2 -Value "Data2"
$objTemp | Add-Member -type NoteProperty -Name Column3 -Value "Data3"
$objTemp | Add-Member -type NoteProperty -Name Column4 -Value "Data4"
$objTemp | Add-Member -type NoteProperty -Name Column5 -Value "Data5"
$objTemp | Add-Member -type NoteProperty -Name Column6 -Value "Data6"
$objTemp | Add-Member -type NoteProperty -Name Column7 -Value "Data7"
$objTemp | Add-Member -type NoteProperty -Name Column8 -Value "Data8"
$objTemp | Add-Member -type NoteProperty -Name Column9 -Value "Data9"
$objTemp | Add-Member -type NoteProperty -Name Column10 -Value "Data10"
$objTemp | Add-Member -type NoteProperty -Name Column11 -Value "Data11"
$objTemp | Add-Member -type NoteProperty -Name Column12 -Value "Data12"
$objTemp | Add-Member -type NoteProperty -Name Column13 -Value "Data13"
$objTemp | Add-Member -type NoteProperty -Name Column14 -Value "Data14"
$objTemp | Add-Member -type NoteProperty -Name Column15 -Value "Data15"
$aryTemp += $objTemp

#only shows 10 columns
$aryTemp | Format-Table

#show all
$aryTemp | Format-Table *

输出:

Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10
------- ------- ------- ------- ------- ------- ------- ------- ------- --------
Data1   Data2   Data3   Data4   Data5   Data6   Data7   Data8   Data9   Data10  



Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10 Column11 Column12 Column13 Column14 Column15
------- ------- ------- ------- ------- ------- ------- ------- ------- -------- -------- -------- -------- -------- --------
Data1   Data2   Data3   Data4   Data5   Data6   Data7   Data8   Data9   Data10   Data11   Data12   Data13   Data14   Data15  

哎呀,现在我的许多列都被省略号(...)字符截断了,请参见https://gist.github.com/jpluimers/10c41bb23f1c7361f0ec268ecb378874的输出。我能同时得到最好的两个世界吗? - Jeroen Wiert Pluimers
你是用PowerShell.exe还是IDE来运行这段代码?你能提供一个列表输出吗(以便不被截断)?另外,你的代码输出是什么样子的? - Phry
我已经尝试过了,但它仍然包装了最后一列:https://gist.github.com/jpluimers/10c41bb23f1c7361f0ec268ecb378874 - Jeroen Wiert Pluimers
1
在这种情况下,您需要将“-Width”添加到其中。像这样:.\List-Delphi-Installed-Packages.ps1 ProductSummaries | Out-File -FilePath c:\temp\Delphi.txt -Width 500 输出差异:https://gist.github.com/anonymous/f9f45a86c94643e878eb99c647a59c2c - Phry
1
很高兴一切都正常。顺便提一下,可以使用“-PassThru”选项将日志记录到文件中,同时在控制台上显示,例如“Add-Content $Logfile -Value $strTemp -PassThru”。 - Phry
显示剩余6条评论

2

最简单可靠的方法是使用$FormatEnumerationLimit-wrap一起使用。

嵌套的内容较多时,Format-Table -wrap -Autosize仍然会用...来总结内容。

使用一个嵌套对象进行测试:

$hash = [pscustomobject]@{
   'List' = 0.. 100   
   'files' = (Get-ChildItem ~ ).Name
}

$hash | Format-Table -Wrap -AutoSize
$hash | Format-Table -AutoSize

输出:

List          files
----          -----
{0, 1, 2, 3…} {.android, .AndroidStudio, .AndroidStudio2.3, .beautifuldiscord…}


List          files
----          -----
{0, 1, 2, 3…} {.android, .AndroidStudio, .AndroidStudio2.3, .beautifuldiscord…}

展开前10个项目

$FormatEnumerationLimit = 10
$hash | Format-Table -AutoSize -Wrap

List                            files
----                            -----
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9…} {.android, .AndroidStudio, .AndroidStudio2.3,
                                .beautifuldiscord, .cache, .cargo, .config, .dbus-keyrings,
                                .dotnet, .fiddler…}

扩展所有内容$FormatEnumerationLimit = -1

如果太大,使用起来会变得不太方便。

List
----
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 100}

Format-List-1 结合使用效果更佳。

Get-Module | select Export* | Format-List

Format-Table 是在 15 左右表现良好的命令。

将超级宽的表格导入到 VS Code

$FormatEnumerationLimit = -1
Get-Module | format-table -AutoSize  | Out-String -Width 9999

感谢您提供详细的见解。到目前为止,我已经使用了@Phry在被接受的答案中提到的“*”技巧。对于未来的项目,我将尝试您的建议,并思考如何使用全局变量来影响本地问题。https://dev59.com/IIDba4cB1Zd3GeqPGqPA#38794195 https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/formatenumerationlimit-scoping-issues https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7 - Jeroen Wiert Pluimers

0

所以,现在你写了很多关于这个问题的内容,对我来说,*.format.ps1xml文件中给出了C:\Windows\System32\WindowsPowerShell\v1.0Format-table的行为。但我找不到PSObjectPSCustomObject(所以默认情况下最多只有9个)。

使用Format-Table-property参数,您可以按您想要的顺序显示任意数量的属性。

也许您可以使用编写 Windows PowerShell 格式文件,这是关于此主题的Microsoft文档,来构建自己的格式文件。

我处理您的问题的方法是创建一个C#类(类型),使用Add-Type插入它,然后提供一个类型文件和一个格式文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接