Pipe results can be grouped by using Sort-object and Group-object.
In fact, the results of the execution of each command has been sorted out. For example, to view a list of files through LS, the default is to sort by the Name property, but you can sort by specifying attributes such as:
PS c:powershell> ls | Sort-object length
Mode lastwritetime length Name
---- ------------------------a
---2011/11/ 15:30 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
This defaults to ascending sorting based on length, and if you want to arrange in descending order, use the descending option.
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
To sort objects and hash tables
If you want to complete the primary keyword descending order, the sort of secondary keyword ascending, the first thing you might think of is:
PS c:powershell> Dir | Sort-object Length, name-descending,-ascending
sort-object: No positional arguments were found to accept the actual parameter "system.object[".
Location: 1 characters:
+ Dir | Sort-object <<<< Length, name-descending,-ascending
+ categoryinfo : invalidargument: (:) [Sort-object], Parameterbin
dingexception
+ fullyqualifiederrorid:positionalparameternotfound, Microsoft.PowerShell
. Commands.sortobjectcommand
But the above method does not work, but this operation:
PS c:powershell> Dir | Sort-object @{expression= "Length";D escending= $true},@{ex
pression= "Name"; ascending= $true}
directory: 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
grouping data
If you want to view all services that are currently closed and turned on, and group by state. But use:
PS c:powershell> Get-service | Group-object Status
Count Name Group
--------- -----
Running { System.ServiceProcess.ServiceController, SYSTEM.SERVICEPROCESS.S
Ervicecontroller, System.ServiceProcess.ServiceController, System
. Serviceprocess.servicecontroller ...}
Stopped {System.ServiceProcess.ServiceController, system.serviceprocess.s
Ervicecontroller, System.ServiceProcess.ServiceController, System
. Serviceprocess.servicecontroller ...}
For example, the current directory files are grouped by extension .
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}
Grouping by using an expression
If you want to view the files for the current directory, group the file size by more than 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 ...}
If you grouped by the first letter of the file name
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}
Group by publisher of the current application
PS c:powershell> get-process | Group-object company-noelement
Count Name
---------
2 Adobe Systems incorpor ...
2 Microsoft
Corporation
1 Adobe Systems, Inc.
1 Microsoft (R) Corporation
1
1 NVIDIA Corporation
grouping with formatting commands
Group-object is not the only command that can complete the grouping function, in fact, formatting commands such as Format-object support a groupby parameter, or they can complete the grouping.
PS c:powershell> Dir | Sort-object Extension, Name | Format-table-groupby Extension
directory: C:powershell
Mode lastwritetime Length Name
---- ----- -------- ----------
-a--- 2011/11/24 20:26 12060 alias
directory: C:powershell
Mode LastWriteTime Length Name
---- ------------- -----------a
--- 2011/11/28 15:30 ping.bat
Table of Contents: C:powershell
Mode lastwritetime Length Name
---- ------------- -----------a
--- 2011/11/ 18:30 67580 a.html-a
--- 2011/12/14 11:22 3460-a
--- ls.html 17:37 7420 name.html-a
--- 2011/11/24 17:44 735892 powershell_cmdlets.html -A
--- 2011/11/30 16:04 2556 psdrive.html