使用 Format 命令來變更輸出檢視

PowerShell 有一組 Cmdlet,可讓您控制特定物件的屬性顯示方式。 所有 Cmdlet 的名稱會以動詞 開頭 Format。 它們可讓您選取您想要顯示的屬性。

Get-Command -Verb Format -Module Microsoft.PowerShell.Utility
CommandType     Name               Version    Source
-----------     ----               -------    ------
Cmdlet          Format-Custom      6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Hex         6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-List        6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Table       6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Wide        6.1.0.0    Microsoft.PowerShell.Utility

本文說明 Format-WideFormat-ListFormat-Table Cmdlet。

PowerShell 中的每個物件類型都有當您未選取要顯示的屬性時所使用的預設屬性。 每個 Cmdlet 都會 使用相同的 Property 參數來指定您想要顯示的屬性。 因為 Format-Wide 只會顯示單一屬性,因此其 Property 參數只會接受單一值,但 和的屬性參數 Format-List 接受 Format-Table 屬性名稱清單。

在此範例中 Get-Process ,Cmdlet 的默認輸出會顯示我們執行 Internet Explorer 的兩個實例。

Get-Process -Name iexplore

Process 物件的預設格式會顯示此處顯示的屬性:

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     32    25.52      10.25      13.11   12808   1 iexplore
     52    11.46      26.46       3.55   21748   1 iexplore

針對單一項目輸出使用 Format-Wide

Cmdlet Format-Wide 預設只會顯示 對象的預設屬性。 與每個物件相關聯的資訊會顯示在單一資料列中:

Get-Command -Verb Format | Format-Wide
Format-Custom          Format-Hex
Format-List            Format-Table
Format-Wide

您也可以指定非預設屬性:

Get-Command -Verb Format | Format-Wide -Property Noun
Custom                 Hex
List                   Table
Wide

使用資料行控制全格式顯示器

Format-Wide使用 Cmdlet,您一次只能顯示單一屬性。 這可讓您在多個資料行中顯示大型清單。

Get-Command -Verb Format | Format-Wide -Property Noun -Column 3
Custom                 Hex                  List
Table                  Wide

針對清單檢視使用 Format-List

Cmdlet 會 Format-List 以清單的形式顯示物件,每個屬性都會加上標籤並顯示在個別行上:

Get-Process -Name iexplore | Format-List
Id      : 12808
Handles : 578
CPU     : 13.140625
SI      : 1
Name    : iexplore

Id      : 21748
Handles : 641
CPU     : 3.59375
SI      : 1
Name    : iexplore

您可以視需要指定多個屬性:

Get-Process -Name iexplore | Format-List -Property ProcessName,FileVersion,StartTime,Id
ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime   : 10/22/2019 11:23:58 AM
Id          : 12808

ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime   : 10/22/2019 11:23:57 AM
Id          : 21748

使用 Format-List 搭配通配符取得詳細資訊

Cmdlet Format-List 可讓您使用通配符作為其 Property 參數的值。 這可讓您顯示詳細資訊。 物件通常包含比您需要更多的資訊,這就是為什麼 PowerShell 預設不會顯示所有屬性值的原因。 若要顯示物件的所有屬性,請使用 Format-List -Property * 命令。 下列命令會產生超過 60 行輸出的單一進程:

Get-Process -Name iexplore | Format-List -Property *

Format-List雖然命令對於顯示詳細數據很有用,但如果您想要包含許多項目的輸出概觀,則更簡單的表格式檢視通常更有用。

針對表格式輸出使用 Format-Table

如果您使用 Format-Table 沒有指定屬性名稱的 Cmdlet 來格式化命令的 Get-Process 輸出,則會取得與不使用 Cmdlet 完全相同的 Format 輸出。 根據預設,PowerShell 會以表格式格式顯示 Process 物件。

Get-Service -Name win* | Format-Table
Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running  Winmgmt            Windows Management Instrumentation
Running  WinRM              Windows Remote Management (WS-Manag...

注意

Get-Service 僅適用於 Windows 平臺。

改善 Format-Table 輸出

雖然表格式檢視很適合用來顯示許多資訊,但如果顯示太窄,數據可能很難解譯。 在上一個範例中,輸出會截斷。 如果您在執行Format-Table命令時指定 AutoSize 參數,PowerShell 會根據顯示的實際數據來計算資料行寬度。 這可讓數據行讀取。

Get-Service -Name win* | Format-Table -AutoSize
Status  Name                DisplayName
------  ----                -----------
Running WinDefend           Windows Defender Antivirus Service
Running WinHttpAutoProxySvc WinHTTP Web Proxy Auto-Discovery Service
Running Winmgmt             Windows Management Instrumentation
Running WinRM               Windows Remote Management (WS-Management)

Format-Table Cmdlet 可能仍會截斷數據,但只會截斷畫面結尾。 除了最後一個顯示的屬性,會提供與其最長的數據元素正確顯示所需的大小一樣大。

Get-Service -Name win* |
    Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -AutoSize
Name                 Status StartType DisplayName                               DependentServi
                                                                                ces
----                 ------ --------- -----------                               --------------
WinDefend           Running Automatic Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc, iphl…
Winmgmt             Running Automatic Windows Management Instrumentation        {vmms, TPHKLO…
WinRM               Running Automatic Windows Remote Management (WS-Management) {}

命令 Format-Table 假設屬性會依重要性順序列出。 Cmdlet 會嘗試完整顯示最接近開頭的屬性。 Format-Table如果命令無法顯示所有屬性,則會從顯示中移除一些數據行。 您可以在上一個範例的 DependentServices 屬性中看到此行為。

在數據行中包裝 Format-Table 輸出

您可以使用 Wrap 參數,強制冗長 Format-Table 的數據在其顯示數據行 內換 行。 使用 Wrap 參數可能無法執行您預期的情況,因為如果您未指定 AutoSize,則會使用預設設定:

Get-Service -Name win* |
    Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -Wrap
Name                 Status StartType DisplayName                               DependentServi
                                                                                ces
----                 ------ --------- -----------                               --------------
WinDefend           Running Automatic Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc,
                                                                                iphlpsvc}
Winmgmt             Running Automatic Windows Management Instrumentation        {vmms,
                                                                                TPHKLOAD,
                                                                                SUService,
                                                                                smstsmgr…}
WinRM               Running Automatic Windows Remote Management (WS-Management) {}

單獨使用 Wrap 參數並不會讓處理速度變慢。 不過,使用 AutoSize 來格式化大型目錄結構的遞歸檔案清單可能需要很長的時間,並在顯示第一個輸出專案之前使用大量的記憶體。

如果您不關心系統負載,則 AutoSize 適用於 Wrap 參數。 初始數據行仍視需要使用盡可能多的寬度來顯示單行上的專案,但必要時會包裝最後一欄。

注意

當您先指定最寬的數據行時,可能不會顯示某些數據行。 為了獲得最佳結果,請先指定最小的數據元素。

在下列範例中,我們會先指定最寬的屬性。

Get-Process -Name iexplore |
    Format-Table -Wrap -AutoSize -Property FileVersion, Path, Name, Id

即使換行,也會省略最終標識符數據行:

FileVersion                          Path                                                  Nam
                                                                                           e
-----------                          ----                                                  ---
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE iex
                                                                                           plo
                                                                                           re
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files\Internet Explorer\iexplore.exe       iex
                                                                                           plo
                                                                                           re

組織數據表輸出

表格式輸出控件的另一個實用參數是 GroupBy。 特別表格式清單可能很難比較。 GroupBy 參數會根據屬性值來分組輸出。 例如,我們可以依 StartType 將服務分組,以方便檢查,省 略屬性清單中的 StartType 值:

Get-Service -Name win* | Sort-Object StartType | Format-Table -GroupBy StartType
   StartType: Automatic
Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  Winmgmt            Windows Management Instrumentation
Running  WinRM              Windows Remote Management (WS-Managem…

   StartType: Manual
Status   Name               DisplayName
------   ----               -----------
Running  WinHttpAutoProxyS… WinHTTP Web Proxy Auto-Discovery Serv…