PowerShell Format-Table的細節(AutoSize和Wrap參數)
阿新 • • 發佈:2019-04-28
process 命令 prop 屬性。 rem code 顯示 關心 列表 我們在使用Format-Table去顯示輸出信息的時候,通常會發現如果某個屬性字段比較長,則無法正常全部顯示,如下。
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 命令無法顯示所有屬性,它將從顯示中刪除某些列,並發出警告。如果你使名稱變成列表中的最後一個屬性,便可以看到這一行為:
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
你還可以通過使用 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 參數則會獲得良好的效果。
PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Name,Id,Company,Path
Name Id Company Path
---- -- ------- ----
powershell 2836 Microsoft Corporation C:\Program Files\Windows PowerShell\v1.0
powershell.exe
如果你先指定最寬的列,則某些列可能無法顯示,因此最安全的做法是先指定最小的數據元素。在下面的示例中,我們首先指定特別寬的路徑元素,甚至使用自動換行,但仍丟失了最後的名稱列:
PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Path,Id,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 Corporation
PowerShell Format-Table的細節(AutoSize和Wrap參數)