我們可以利用 Sort-Object Cmdlet 組織顯示的資料,使其更易於掃描。Sort-Object 接受所要排序的一或多個屬性的名稱,並依這些屬性的值將資料排序後傳回。

請回想一下關於 Win32_SystemDriver 執行個體列表的問題。您可以輸入下列命令,先依 State 再依 Name 進行排序:

Get-WmiObject -Class Win32_SystemDriver | Sort-Object -Property State,Name | Format-Table -Property Name,State,Started,DisplayName -AutoSize -Wrap

雖然顯示的清單很長,如今狀態相同的項目都已分成同一組:

Name           State   Started DisplayName
----           -----   ------- -----------
ACPI           Running    True Microsoft ACPI Driver
AFD            Running    True AFD
AmdK7          Running    True AMD K7 Processor Driver
AsyncMac       Running    True RAS Asynchronous Media Driver
...
Abiosdsk       Stopped   False Abiosdsk
ACPIEC         Stopped   False ACPIEC
aec            Stopped   False Microsoft Kernel Acoustic Echo Canceller
...

您也可以指定 Descending 參數將物件的排序順序反轉。排序順序反轉後,名稱將依字母順序遞減排序,數字則依大小遞減排序。

PS> Get-WmiObject -Class Win32_SystemDriver | Sort-Object -Property State,Name -Descending | Format-Table -Property Name,State,Started,DisplayName -AutoSize -Wrap

Name           State   Started DisplayName
----           -----   ------- -----------
WS2IFSL        Stopped   False Windows Socket 2.0 Non-IFS Service Provider Supp
                               ort Environment
wceusbsh       Stopped   False Windows CE USB Serial Host Driver...
...
wdmaud         Running    True Microsoft WINMM WDM Audio Compatibility Driver
Wanarp         Running    True Remote Access IP ARP Driver
...




目錄