可以使用 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 ...