Windows PowerShell 提供一組 Cmdlet 讓您控制特定物件所要顯示的屬性。這些 Cmdlet 的名稱都是以動詞 Format 開頭。您可藉此選取一或多個屬性加以顯示。
Format Cmdlet 包括 Format-Wide、Format-List、Format-Table 和 Format-Custom。本使用手冊中只介紹 Format-Wide、Format-List 和 Format-Table Cmdlet。
每一個格式化 Cmdlet 都有預設屬性,以便於未指定任何顯示的屬性時使用。每個 Cmdlet 也都使用相同的參數名稱 Property,讓您指定所要顯示的屬性。由於 Format-Wide 只顯示單一屬性,其 Property 參數只接受單一值,但 Format-List 和 Format-Table 的屬性參數則接受屬性名稱清單。
如果目前有 2 個 Windows PowerShell 執行個體正在執行,使用 Get-Process -Name powershell 會顯示下列輸出:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 995 9 30308 27996 152 2.73 2760 powershell 331 9 23284 29084 143 1.06 3448 powershell
本節剩餘的部分將探討如何使用 Format Cmdlet 來變更上述命令顯示輸出的方式。
使用 Format-Wide 顯示單一項目輸出
預設情況下,Format-Wide Cmdlet 只顯示物件的預設屬性。與每個物件有關的資訊顯示成一欄:
PS> Get-Process -Name powershell | Format-Wide powershell powershell
您也可以指定非預設屬性:
PS> Get-Process -Name powershell | Format-Wide -Property Id 2760 3448
使用欄數控制 Format-Wide 顯示結果
使用 Format-Wide Cmdlet 一次只能顯示一個屬性,所以很適合用來顯示每一行只列出一個元素的簡易清單。若要取得這類簡易清單,請將 Column 參數的值設定為 1,像這樣:
Get-Command Format-Wide -Property Name -Column 1
使用 Format-List 顯示清單檢視
Format-List Cmdlet 會以清單格式顯示物件,各用一行標示並顯示每個屬性:
PS> Get-Process -Name powershell | Format-List Id : 2760 Handles : 1242 CPU : 3.03125 Name : powershell Id : 3448 Handles : 328 CPU : 1.0625 Name : powershell
您可以視需要指定多個屬性:
PS> Get-Process -Name powershell | Format-List -Property ProcessName,FileVersion ,StartTime,Id ProcessName : powershell FileVersion : 1.0.9567.1 StartTime : 2006-05-24 13:42:00 Id : 2760 ProcessName : powershell FileVersion : 1.0.9567.1 StartTime : 2006-05-24 13:54:28 Id : 3448
使用 Format-List 搭配萬用字元取得詳細資訊
Format-List Cmdlet 的 Property 參數值可以使用萬用字元。您可藉此顯示詳細資訊。通常,物件包含的資訊未必都是您所需要的,這也就是為何 Windows PowerShell 預設並未顯示所有的屬性值。若要顯示物件的所有屬性,請使用 Format-List -Property * 命令。下列命令只對單一處理序執行,卻會產生 60 行以上的輸出:
Get-Process -Name powershell | Format-List -Property *
使用 Format-List 命令顯示詳細資訊雖很方便,但若您想取得含有許多項目的輸出總覽,則較簡單的表格式檢視通常更實用。
使用 Format-Table 顯示表格式輸出
使用 Format-Table Cmdlet 時如果沒有指定屬性名稱將 Get-Process 命令的輸出格式化,輸出結果便與毫無任何格式化的情形完全一樣。這是因為處理序通常顯示成表格式格式,就如同大多數的 Windows PowerShell 物件。
PS> Get-Process -Name powershell | Format-Table Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 1488 9 31568 29460 152 3.53 2760 powershell 332 9 23140 632 141 1.06 3448 powershell
改良 Format-Table 輸出結果 (AutoSize)
雖然表格式檢視很適合顯示可供比較的大量資訊,如果畫面太窄以致資料放不下就會造成解讀困難。例如,若您試著顯示處理序路徑、識別碼、名稱和公司,輸出的處理序路徑和公司欄將會被截斷:
PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company Path Name Id Company ---- ---- -- ------- C:\Program Files... powershell 2836 Microsoft Corpor...
執行 Format-Table 命令時如果指定 AutoSize 參數,Windows PowerShell 便會依據實際即將顯示的資料來計算欄寬。這樣您就能夠讀取 Path 欄,但 Company 欄仍會被截斷:
PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company - AutoSize Path Name Id Company ---- ---- -- ------- C:\Program Files\Windows PowerShell\v1.0\powershell.exe powershell 2836 Micr...
Format-Table Cmdlet 可能還是會截斷資料,但截斷的部分僅限於螢幕右端的地方。屬性 (顯示在最後面的除外) 是以能夠正確顯示當中最長的資料元素為準決定欄寬。如果您將 Property 值清單中的 Path 和 Company 互換位置,即可看到完整的公司名稱但是路徑被截斷:
PS> Get-Process -Name powershell | Format-Table -Property Company,Name,Id,Path - AutoSize Company Name Id Path ------- ---- -- ---- Microsoft Corporation powershell 2836 C:\Program Files\Windows PowerShell\v1...
Format-Table 命令認定愈接近屬性清單開頭的屬性愈重要。因此,命令會試著完整顯示最接近開頭的屬性。如果 Format-Table 命令無法顯示全部屬性,便會從畫面上移除幾欄並提出警告。將 Name 屬性置於清單結尾就可看到這種現象:
PS> Get-Process -Name powershell | Format-Table -Property Company,Path,Id,Name - AutoSize WARNING: column "Name" does not fit into the display and was removed. Company Path I d ------- ---- - Microsoft Corporation C:\Program Files\Windows PowerShell\v1.0\powershell.exe 6
在上列輸出中,ID 欄為配合清單而被截斷,欄標題則變成堆疊顯示。自動調整欄寬不一定能得到您想要的結果。
將 Format-Table 輸出的欄內容換行 (Wrap)
您可以使用 Wrap 參數將顯示欄內較長的 Format-Table 資料強制換行。單獨使用 Wrap 參數未必會產生預期的結果,因為只要沒有一併指定 AutoSize,此參數將採用預設設定:
PS> Get-Process -Name powershell | Format-Table -Wrap -Property Name,Id,Company, Path Name Id Company Path ---- -- ------- ---- powershell 2836 Microsoft Corporati C:\Program Files\Wi on ndows PowerShell\v1 .0\powershell.exe
單獨使用 Wrap 參數的好處是不致嚴重減緩處理速度。假設您以遞迴方式處理大型目錄系統進而顯示檔案列表,這時若使用 AutoSize,可能就得耗用大量記憶體並且等待很久才會顯示第一個輸出項目。
如果您不擔心系統負荷,則可使用 AutoSize 搭配 Wrap 參數達到不錯的效果。前幾欄會配置到足以將項目顯示成一行的寬度,如同僅指定 AutoSize 而沒有 Wrap 參數的時候一樣。唯一的差別是最後一欄將於必要時換行:
PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Name,I d,Company,Path Name Id Company Path ---- -- ------- ---- powershell 2836 Microsoft Corporation C:\Program Files\Windows PowerShell\v1.0\ powershell.exe
若是先指定最寬的欄,某幾欄可能就不會顯示,因此先指定最小的資料元素最保險。以下範例先指定超寬的路徑元素,導致即使換行也無法顯示最後一欄 Name:
PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Path,I d,Company,Name WARNING: column "Name" does not fit into the display and was removed. Path Id Company ---- -- ------- C:\Program Files\Windows PowerShell\v1.0\powershell.exe 2836 Microsoft Corporat ion
將表格輸出分組 (-GroupBy)
可用於控制表格式輸出的另一個參數是 GroupBy。過長的表格式清單尤其難以比較資訊。GroupBy 參數會依據屬性值將輸出分組。例如,您可將處理序依公司分組以便於檢閱,並從屬性清單中剔除公司值:
PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Name,I d,Path -GroupBy Company Company: Microsoft Corporation Name Id Path ---- -- ---- powershell 1956 C:\Program Files\Windows PowerShell\v1.0\powershell.exe powershell 2656 C:\Program Files\Windows PowerShell\v1.0\powershell.exe