• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Windows Powershell排序和分组管道结果

Windows Powershell排序和分组管道结果

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了windows powershell,windowspowershell2.0,windows powershell64,windows10 powershell,windows7 powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

使用Sort-Object和Group-Object可以对管道结果进行分组。
其实每条命令执行后的结果已经排过序了。例如通过ls 查看文件列表,默认会根据Name属性进行排序,但是你可以通过指定属性进行排序例如:

PS C:Powershell> ls | Sort-Object Length
Mode     LastWriteTime Length Name
----     ------------- ------ ----
-a--- 2011/11/28   15:30   63 ping.bat
-a--- 2011/12/2   18:47  140 test.ps1
-a--- 2011/11/28   16:42  170 test.vbs
-a--- 2011/11/28   11:12  186 LogoTestConfig.xml
-a--- 2011/11/23   17:37  242 test.txt
-a--- 2011/11/25   11:20  556 employee.xml
</div>

这样默认会根据length进行升序排序,如果要降序排列,可是使用Descending选项。

PS C:Powershell> ls | Sort-Object Length -Descending
Mode     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:37  7420 name.html
</div>

给对象和哈希表进行排序

如果要完成主要关键字降序,次要关键字升序的排序,可能首先想到的是:

PS C:Powershell> Dir | Sort-Object Length, Name -descending, -ascending
Sort-Object : 找不到接受实际参数“System.Object[]”的位置形式参数。
所在位置 行:1 字符: 18
+ Dir | Sort-Object <<<< Length, Name -descending, -ascending
  + CategoryInfo     : InvalidArgument: (:) [Sort-Object], ParameterBin
  dingException
  + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
  .Commands.SortObjectCommand
</div>

但是上面的方法行不通,可是这样操作:

PS C:Powershell> Dir | Sort-Object @{expression="Length";Descending=$true},@{ex
pression="Name";Ascending=$true}

  目录: C:Powershell

Mode     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:37  7420 name.html
-a--- 2011/12/14   11:22  3460 ls.html
-a--- 2011/11/30   16:04  2556 psdrive.html
-a--- 2011/11/25   11:20  556 employee.xml
-a--- 2011/11/23   17:37  242 test.txt
-a--- 2011/11/28   11:12  186 LogoTestConfig.xml
-a--- 2011/11/28   16:42  170 test.vbs
-a--- 2011/12/2   18:47  140 test.ps1

</div>

对数据进行分组

如果想查看当前关闭和开启的所有服务,并且通过状态进行分组。可是使用:

PS C:Powershell> Get-Service | Group-Object Status
Count Name  Group
----- ----  -----
  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...}
</div>

再举一例,把当前目录的文件以扩展名进行分组。

PS C:Powershell> ls | Group-Object Extension
Count 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}
</div>

使用表达式分组

如果要查看当前目录的文件,根据文件的大小是否大于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...}

</div>

如果按照文件名的首字母分组

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}
</div>

根据当前应用程序的发布者分组

PS C:Powershell> Get-Process | Group-Object Company -NoElement

Count Name
----- ----
  2 Adobe Systems Incorpor...
  52
  2 微软
  22 Microsoft Corporation
  1 Adobe Systems, Inc.
  1 Microsoft (R) Corporation
  1
  1 NVIDIA Corporation

</div>

使用格式化命令分组

Group-Object并不是唯一可以完成分组功能的命令,事实上格式化命令例如Format-Object支持一个GroupBy的参数,也可以完成分组。

PS C:Powershell> Dir | Sort-Object Extension, Name | Format-Table -groupBy Extension

  目录: C:Powershell

Mode        LastWriteTime   Length Name
----        -------------   ------ ----
-a---    2011/11/24   20:26   12060 alias

  目录: C:Powershell

Mode        LastWriteTime   Length Name
----        -------------   ------ ----
-a---    2011/11/28   15:30     63 ping.bat

  目录: C:Powershell

Mode        LastWriteTime   Length Name
----        -------------   ------ ----
-a---    2011/11/24   18:30   67580 a.html
-a---    2011/12/14   11:22    3460 ls.html
-a---    2011/11/24   17:37    7420 name.html
-a---    2011/11/24   17:44   735892 Powershell_Cmdlets.html
-a---    2011/11/30   16:04    2556 psdrive.html
</div>

</div>

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • Powershell 获取特定的网页信息的代码
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • 简单谈谈PowerShell 4.0中的新命令

相关文章

  • PowerShell中使用Filter来创建管道输入函数
  • PowerShell调用Web测试工具Selenium实例
  • Powershell小技巧之使用Update-TypeData扩展类型系统
  • PowerShell: Try...Catch...Finally 实现方法
  • PowerShell实现参数互斥示例
  • PowerShell重启服务命令Restart-Service详细介绍
  • PowerShell中使用ArrayList实现数组插入、删除、添加例子
  • PowerShell中删除空格、点号、减号和换行方法代码实例
  • 揭秘PowerShell 5.0新特性和新功能
  • Windows Powershell Where-Object 条件过滤

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Windows Powershell分析和比较管道结果
    • PowerShell获取系统环境变量的方法
    • Windows Powershell排序和分组管道结果
    • Powershell小技巧之复合筛选
    • PowerShell小技巧之读取Windows产品密钥
    • PowerShell小技巧之定时抓取屏幕图像
    • PowerShell是什么?
    • PowerShell中定义多个变量并赋值的例子
    • PowerShell捕获错误的2种方法(异常捕获命令、错误变量)
    • Powershell小技巧之屏蔽输出结果

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有