Welcome

首页 / 编程脚本 / Windows Powershell排序和分组管道结果

使用Sort-Object和Group-Object可以对管道结果进行分组。
其实每条命令执行后的结果已经排过序了。例如通过ls 查看文件列表,默认会根据Name属性进行排序,但是你可以通过指定属性进行排序例如:
PS C:Powershell> ls | Sort-Object LengthMode LastWriteTime Length Name---- ------------- ------ -----a--- 2011/11/28 15:30 63 ping.bat-a--- 2011/12/2 18:47140 test.ps1-a--- 2011/11/28 16:42170 test.vbs-a--- 2011/11/28 11:12186 LogoTestConfig.xml-a--- 2011/11/23 17:37242 test.txt-a--- 2011/11/25 11:20556 employee.xml
这样默认会根据length进行升序排序,如果要降序排列,可是使用Descending选项。
PS C:Powershell> ls | Sort-Object Length -DescendingMode LastWriteTime Length Name---- ------------- ------ -----a--- 2011/11/24 17:44 735892 Powershell_Cmdlets.html-a--- 2011/11/24 18:30 67580 a.html-a--- 2011/11/24 20:04 26384 a.txt-a--- 2011/11/29 19:23 21466 function.ps1-a--- 2011/11/24 20:26 12060 alias-a--- 2011/11/24 17:377420 name.html
给对象和哈希表进行排序
如果要完成主要关键字降序,次要关键字升序的排序,可能首先想到的是:
PS C:Powershell> Dir | Sort-Object Length, Name -descending, -ascendingSort-Object : 找不到接受实际参数“System.Object[]”的位置形式参数。所在位置 行:1 字符: 18+ Dir | Sort-Object <<<< Length, Name -descending, -ascending+ CategoryInfo : InvalidArgument: (:) [Sort-Object], ParameterBindingException+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SortObjectCommand
但是上面的方法行不通,可是这样操作:
PS C:Powershell> Dir | Sort-Object @{expression="Length";Descending=$true},@{expression="Name";Ascending=$true}目录: C:PowershellMode LastWriteTime Length Name---- ------------- ------ -----a--- 2011/11/24 17:44 735892 Powershell_Cmdlets.html-a--- 2011/11/24 18:30 67580 a.html-a--- 2011/11/24 20:04 26384 a.txt-a--- 2011/11/29 19:23 21466 function.ps1-a--- 2011/11/24 20:26 12060 alias-a--- 2011/11/24 17:377420 name.html-a--- 2011/12/14 11:223460 ls.html-a--- 2011/11/30 16:042556 psdrive.html-a--- 2011/11/25 11:20556 employee.xml-a--- 2011/11/23 17:37242 test.txt-a--- 2011/11/28 11:12186 LogoTestConfig.xml-a--- 2011/11/28 16:42170 test.vbs-a--- 2011/12/2 18:47140 test.ps1
对数据进行分组
如果想查看当前关闭和开启的所有服务,并且通过状态进行分组。可是使用:
PS C:Powershell> Get-Service | Group-Object StatusCount NameGroup----- ---------87 Running {System.ServiceProcess.ServiceController, System.ServiceProcess.S erviceController, System.ServiceProcess.ServiceController, System .ServiceProcess.ServiceController...}88 Stopped {System.ServiceProcess.ServiceController, System.ServiceProcess.S erviceController, System.ServiceProcess.ServiceController, System .ServiceProcess.ServiceController...}
再举一例,把当前目录的文件以扩展名进行分组
PS C:Powershell> ls | Group-Object ExtensionCount Name Group----- ---- -----2{ABC, alias}5 .html {a.html, ls.html, name.html, Powershell_Cmdlets.html...}2 .txt {a.txt, test.txt}2 .xml {employee.xml, LogoTestConfig.xml}2 .ps1 {function.ps1, test.ps1}1 .bat {ping.bat}1 .vbs {test.vbs}
使用表达式分组
如果要查看当前目录的文件,根据文件的大小是否大于1kb分组。
PS C:Powershell> ls | Group-Object {$_.Length -gt 1kb}Count Name Group----- ---- -----7 False {ABC, employee.xml, LogoTestConfig.xml, ping...8 True {a.html, a.txt, alias, function.ps1...}
如果按照文件名的首字母分组
PS C:Powershell> ls | Group-Object {$_.name.SubString(0,1).toUpper()}Count Name Group----- ---- -----3 A{a.html, a.txt, alias}1 E{employee.xml}1 F{function.ps1}2 L{LogoTestConfig.xml, ls.html}1 N{name.html}3 P{ping.bat, Powershell_Cmdlets.html, psdrive.html}3 T{test.ps1, test.txt, test.vbs}
根据当前应用程序的发布者分组
PS C:Powershell> Get-Process | Group-Object Company -NoElementCount Name----- ----2 Adobe Systems Incorpor...522 微软22 Microsoft Corporation1 Adobe Systems, Inc.1 Microsoft (R) Corporation11 NVIDIA Corporation
使用格式化命令分组
Group-Object并不是唯一可以完成分组功能的命令,事实上格式化命令例如Format-Object支持一个GroupBy的参数,也可以完成分组。
PS C:Powershell> Dir | Sort-Object Extension, Name | Format-Table -groupBy Extension目录: C:PowershellModeLastWriteTime Length Name----------------- ------ -----a---2011/11/24 20:26 12060 alias目录: C:PowershellModeLastWriteTime Length Name----------------- ------ -----a---2011/11/28 15:30 63 ping.bat目录: C:PowershellModeLastWriteTime Length Name----------------- ------ -----a---2011/11/24 18:30 67580 a.html-a---2011/12/14 11:223460 ls.html-a---2011/11/24 17:377420 name.html-a---2011/11/24 17:44 735892 Powershell_Cmdlets.html-a---2011/11/30 16:042556 psdrive.html