如果你在没有指定属性名称的情况下使用 Format-Table cmdlet 来格式化 Get-Process 命令的输出,你所得到的输出会和未使用 Format cmdlet 执行任何格式设置时得到的完全一样。 默认情况下,PowerShell 以表格格式显示 Process 对象 。

PS C:\Users\maxsu> Get-Service -Name win* | Format-Table

Status   Name               DisplayName
------   ----               -----------
Stopped  WinDefend          Windows Defender Antivirus Service
Running  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running  Winmgmt            Windows Management Instrumentation
Stopped  WinRM              Windows Remote Management (WS-Manag...

改进 Format-Table 输出(自动调整大小)
尽管表格视图对显示大量信息很有用,但如果显示区域对于数据来说太窄,则可能导致数据难以理解。 在上面的示例中,输出被截断。 当你运行 Format-Table 命令时,如果你指定 AutoSize 参数,PowerShell 会根据显示的实际数据来计算列宽。 这使得列可读。

PS C:\Users\maxsu> Get-Service -Name win* | Format-Table -AutoSize

Status  Name                DisplayName
------  ----                -----------
Stopped WinDefend           Windows Defender Antivirus Service
Running WinHttpAutoProxySvc WinHTTP Web Proxy Auto-Discovery Service
Running Winmgmt             Windows Management Instrumentation
Stopped WinRM               Windows Remote Management (WS-Management)

Format-Table cmdlet 仍可能会截断数据,但仅会在屏幕的末尾处进行截断。 除最后一个显示的属性外,会让所有属性获得各自所需的大小,以使最长的数据元素得以正确显示。

PS C:\Users\maxsu> Get-Service -Name win* | Format-Table -Property Name,Status,StartType,DisplayName,DependentServices -AutoSize

Name                 Status StartType DisplayName                               DependentServices
----                 ------ --------- -----------                               -----------------
WinDefend           Stopped    Manual Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc, iphlpsvc}
Winmgmt             Running Automatic Windows Management Instrumentation        {VMwareHostd, VMUSBArbService, VMAut...
WinRM               Stopped    Manual Windows Remote Management (WS-Management) {}

Format-Table 命令假设按重要性顺序列出属性。 因此,它会尝试完整显示离列表开头最近的那些属性。 如果 Format-Table 命令无法显示所有属性,则会从显示中删除某些列。 可以在 DependentServices 属性前一个示例中查看此行为 。
让列中的 Format-Table 输出自动换行 (Wrap)
可以通过使用 Wrap 参数让较长的 Format-Table 数据在其显示列中自动换行 。 使用 Wrap 参数可能不会实现所需的操作,因为如果你不同时指定 AutoSize,它会使用默认设置 :

PS C:\Users\maxsu> Get-Service -Name win* | Format-Table -Property Name,Status,StartType,DisplayName,DependentServices -Wrap

Name                 Status StartType DisplayName                               DependentServices
----                 ------ --------- -----------                               -----------------
WinDefend           Stopped    Manual Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc, iphlpsvc}
Winmgmt             Running Automatic Windows Management Instrumentation        {VMwareHostd, VMUSBArbService, VMAuthdS
                                                                                ervice, NcaSvc...}
WinRM               Stopped    Manual Windows Remote Management (WS-Management) {}

使用 Wrap 参数基本不会减慢进程速度 。 但是,使用 AutoSize 格式化大型目录结构的递归文件列表可能得耗用大量时间和内存,才能显示第一批输出项 。
如果你并不关心系统负载,那么结合使用 AutoSize 和 Wrap 参数则会获得良好的效果 。 初始列仍根据需要最大限度地使用宽度来在一行上显示项,但在必要时将最后一列换行。

备注:当你首先指定最宽的列时,可能不会显示某些列。 为了获得最佳结果,请先指定最小的数据元素。

在下面的示例中,我们首先指定最宽的属性。

PS C:\Users\maxsu> Get-Process -Name vm* | Format-Table -Wrap -AutoSize -Property FileVersion,Path,Name,Id

FileVersion Path Name                     Id
----------- ---- ----                     --
                 vmnat                  3988
                 vmnetdhcp              3960
                 vmware-authd           5036
                 vmware-hostd           5724
                 vmware-usbarbitrator64 4432

组织选项卡输出 (-GroupBy)
用于表格输出控制的另一个有用参数是 GroupBy。 较长的列表可能尤其难以进行比较。 GroupBy 参数基于属性值对输出进行分组 例如,我们可以按 StartType 对服务分组以便于检查,同时从属性列表中省略 StartType 值 :

PS C:\Users\maxsu> Get-Service -Name win* | Sort-Object StartType | Format-Table -GroupBy StartType


   StartType:Automatic

Status   Name               DisplayName
------   ----               -----------
Running  Winmgmt            Windows Management Instrumentation


   StartType:Manual

Status   Name               DisplayName
------   ----               -----------
Stopped  WinRM              Windows Remote Management (WS-Manag...
Stopped  WinDefend          Windows Defender Antivirus Service
Running  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...