Windows Powershell sorting and grouping pipeline results example

Windows Powershell sorting and grouping pipeline results example

Pipeline results can be grouped using Sort-Object and Group-Object.
In fact, the results after executing each command have been sorted. For example, if you view the file list through ls, it will be sorted according to the Name attribute by default, but you can specify
Properties to sort:

For example:

PS C:\wang\VB_family\VBA\tool2> ls | Sort-Object Length

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 19:38 48 .gitignore
-a--- 2023/09/28 19:38 315 package.json
-a--- 2023/09/28 19:39 360 config.json
-a--- 2023/09/28 20:42 10366 test.xlsx
-a--- 2023/09/28 20:42 21547 test.xlsm
d---- 2023/09/28 19:39 logs
d---- 2023/09/28 19:38 ribbons
d---- 2023/09/28 19:39 vba-files
d---- 2023/09/28 19:38 xvba_modules
d---- 2023/09/28 19:38 xvba_unit_test

PS C:\wang\VB_family\VBA\tool2>

This will sort by length in ascending order by default. If you want to sort in descending order, you can use the Descending option.

PS C:\wang\VB_family\VBA\tool2> ls | Sort-Object Length -Descending

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 20:42 21547 test.xlsm
-a--- 2023/09/28 20:42 10366 test.xlsx
-a--- 2023/09/28 19:39 360 config.json
-a--- 2023/09/28 19:38 315 package.json
-a--- 2023/09/28 19:38 48 .gitignore
d---- 2023/09/28 19:39 logs
d---- 2023/09/28 19:38 ribbons
d---- 2023/09/28 19:39 vba-files
d---- 2023/09/28 19:38 xvba_modules
d---- 2023/09/28 19:38 xvba_unit_test

PS C:\wang\VB_family\VBA\tool2>

Sort objects and hash tables

If you want to sort the primary keywords in descending order and the secondary keywords in ascending order, the first thing that may come to mind is:

PS C:\wang\VB_family\VBA\tool2> Dir | Sort-Object Length, Name -descending, -ascending
ParserError:
Line |
   1 | Dir | Sort-Object Length, Name -descending, -ascending
     | ~
     | Missing argument in parameter list.
PS C:\wang\VB_family\VBA\tool2>

But the above method doesn’t work, you can do this:

PS C:\wang\VB_family\VBA\tool2> Dir | Sort-Object @{<!-- -->expression="Length";Descending=$true},@{<!-- --> expression="Name";Ascending=$true}

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 20:42 21547 test.xlsm
-a--- 2023/09/28 20:42 10366 test.xlsx
-a--- 2023/09/28 19:39 360 config.json
-a--- 2023/09/28 19:38 315 package.json
-a--- 2023/09/28 19:38 48 .gitignore
d---- 2023/09/28 19:39 logs
d---- 2023/09/28 19:38 ribbons
d---- 2023/09/28 19:39 vba-files
d---- 2023/09/28 19:38 xvba_modules
d---- 2023/09/28 19:38 xvba_unit_test

PS C:\wang\VB_family\VBA\tool2>

Group data

If you want to view all currently closed and opened services and group them by status. But use:

PS C:\wang\VB_family\VBA\tool2> Get-Service | Group-Object Status
Get-Service: Service 'McpManagementService (McpManagementService)' cannot be queried due to the following error:
Get-Service: Service 'NPSMSvc_6e472 (NPSMSvc_6e472)' cannot be queried due to the following error:
Get-Service: Service 'WaaSMedicSvc (WaaSMedicSvc)' cannot be queried due to the following error:

Count Name Group
----- ---- -----
  192 Stopped {<!-- -->AarSvc_6e472, AJRouter, ALG, AntiCheatExpert Service…}
  122 Running {<!-- -->AlibabaProtect, Appinfo, AppXSvc, AudioEndpointBuilder…}

PS C:\wang\VB_family\VBA\tool2>

As another example, group the files in the current directory by extension.

PS C:\wang\VB_family\VBA\tool2> ls | Group-Object Extension

Count Name Group
----- ---- -----
    5 {<!-- -->C:\wang\VB_family\VBA\tool2\logs, C:\wang\VB_family\VBA\tool2\ribbons, C:\wang\VB_family\VBA\tool2\vba-files, C:\wang\VB_family\VBA\tool2\xvba_modules…}
    1 .gitignore {<!-- -->C:\wang\VB_family\VBA\tool2\.gitignore}
    2 .json {<!-- -->C:\wang\VB_family\VBA\tool2\config.json, C:\wang\VB_family\VBA\tool2\package.json}
    1 .xlsm {<!-- -->C:\wang\VB_family\VBA\tool2\test.xlsm}
    1 .xlsx {<!-- -->C:\wang\VB_family\VBA\tool2\test.xlsx}

PS C:\wang\VB_family\VBA\tool2>

Use expression grouping

If you want to view the files in the current directory, group them according to whether the file size is greater than 1kb.

PS C:\wang\VB_family\VBA\tool2> ls | Group-Object {<!-- -->$_.Length -gt 1kb}

Count Name Group
----- ---- -----
    8 False {<!-- -->C:\wang\VB_family\VBA\tool2\logs, C:\wang\VB_family\VBA\tool2\ribbons, C:\wang\VB_family\VBA\tool2\vba-files , C:\wang\VB_family\VBA\tool2\xvba_modules…}
    2 True {<!-- -->C:\wang\VB_family\VBA\tool2\test.xlsm, C:\wang\VB_family\VBA\tool2\test.xlsx}

PS C:\wang\VB_family\VBA\tool2>

If you group by the first letter of the file name

PS C:\wang\VB_family\VBA\tool2> ls | Group-Object {<!-- -->$_.name.SubString(0,1).toUpper()}

Count Name Group
----- ---- -----
    1. {<!-- -->C:\wang\VB_family\VBA\tool2\.gitignore}
    1 C {<!-- -->C:\wang\VB_family\VBA\tool2\config.json}
    1 L {<!-- -->C:\wang\VB_family\VBA\tool2\logs}
    1 P {<!-- -->C:\wang\VB_family\VBA\tool2\package.json}
    1 R {<!-- -->C:\wang\VB_family\VBA\tool2\ribbons}
    2 T {<!-- -->C:\wang\VB_family\VBA\tool2\test.xlsm, C:\wang\VB_family\VBA\tool2\test.xlsx}
    1 V {<!-- -->C:\wang\VB_family\VBA\tool2\vba-files}
    2 X {<!-- -->C:\wang\VB_family\VBA\tool2\xvba_modules, C:\wang\VB_family\VBA\tool2\xvba_unit_test}

PS C:\wang\VB_family\VBA\tool2>

Group by publisher of current application

PS C:\wang\VB_family\VBA\tool2> Get-Process | Group-Object Company -NoElement

Count Name
----- ----
  135
    1
   21Google LLC
    2 Logitech, Inc.
    1 McAfee LLC
    3McAfee, LLC
   85 Microsoft Corporation
    2Microsoft Corporation…
    1Oracle Corporation
    2 Project: Sakura-Editor
    2 Realtek Semiconductor
    4 Sogou.com
    4 Zoom Video Communication…

PS C:\wang\VB_family\VBA\tool2>

Group using 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, and can also
Complete grouping.

PS C:\wang\VB_family\VBA\tool2> Dir | Sort-Object Extension, Name | Format-Table -groupBy Extension

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2023/09/28 19:39 logs
d---- 2023/09/28 19:38 ribbons
d---- 2023/09/28 19:39 vba-files
d---- 2023/09/28 19:38 xvba_modules
d---- 2023/09/28 19:38 xvba_unit_test

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 19:38 48 .gitignore

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 19:39 360 config.json
-a--- 2023/09/28 19:38 315 package.json

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 20:42 21547 test.xlsm

    Directory: C:\wang\VB_family\VBA\tool2

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/09/28 20:42 10366 test.xlsx

PS C:\wang\VB_family\VBA\tool2>