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-Wide
、 Format-List
和 Format-Table
Cmdlet。
PowerShell 中的每個物件類型都有當您未選取要顯示的屬性時所使用的預設屬性。 每個 Cmdlet 都會使用相同的 Property 參數來指定您想要顯示的屬性。 因為 Format-Wide
只會顯示單一屬性,因此其 Property 參數只會接受單一值,但的 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 顯示
使用Format-Wide
Cmdlet,您一次只能顯示一個屬性。 這可讓您在多個資料行中顯示大型清單。
Get-Command -Verb Format | Format-Wide -Property Noun -Column 3
Custom Hex List
Table Wide
列表視圖中使用 Format-List
Cmdlet 會以列表形式顯示物件,每個屬性都會被標示並顯示在各自的行上:
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
即使已經換行,最終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…