powershell

How to sort the output of winget list by column in powershell?


I'm not getting the expected output when trying to sort the output from winget list in powershell. The Id column is not sorted.


# winget list | Sort-Object -Property Id

ScreenToGif                            NickeManarin.ScreenToGif               2.37.1                             winget
Microsoft Visual C++ 2015-2019 Redist… Microsoft.VCRedist.2015+.x64           14.28.29325.2           14.34.318… winget
paint.net                              {28718A56-50EF-4867-B4C8-0860228B5EC9} 4.3.8
Python 3.10.0 (64-bit)                 {21b42743-c8f9-49d7-b8b6-b5855317c7ed} 3.10.150.0
Microsoft Support and Recovery Assist… 0527a644a4ddd31d                       17.0.7018.4
-----------------------------------------------------------------------------------------------------------------------
Name                                   Id                                     Version                 Available  Source
Paint 3D                               Microsoft.MSPaint_8wekyb3d8bbwe        6.2009.30067.0
Microsoft .NET SDK 6.0.402 (x64)       Microsoft.DotNet.SDK.6                 6.0.402                            winget
3D Viewer                              Microsoft.Microsoft3DViewer_8wekyb3d8… 7.2010.15012.0
Microsoft Sticky Notes                 Microsoft.MicrosoftStickyNotes_8wekyb… 3.8.8.0

Q: How can I sort the output of winget list by the Id column in powershell?

I would like to see a powershell solution similar to the Bash sort -k <column-number>, to sort on any column. I fail to see why this obvious function is not available in powershell?


Solution

  • It outputs text, not an object with properties like "Id". This program's output isn't very smart. It looks like it outputs some special characters as well like (U+2026 HORIZONTAL ELLIPSIS). The first thing that occurs to me is to cut off the first 39 characters and then sort it by column 40 onward, where Id starts. That should be like sort -k in unix. I believe a powershell version of winget is coming in the future. Replacing non-ascii with spaces and skipping the first 4 lines.

    # or -creplace '\P{IsBasicLatin}'
    (winget list) -replace '[^ -~]',' ' | select-object -skip 4 | 
      sort-object { $_.substring(39) }
    
    Python 3.10.0 (64-bit)                 {21b42743-c8f9-49d7-b8b6-b5855317c7ed} 3.10.150.0
    paint.net                              {28718A56-50EF-4867-B4C8-0860228B5EC9} 4.3.8
    Microsoft Support and Recovery Assist  0527a644a4ddd31d                       17.0.7018.4
    Name                                   Id                                     Version                 Available  Source
    ScreenToGif                            NickeManarin.ScreenToGif               2.37.1                             winget
    Microsoft .NET SDK 6.0.402 (x64)       Microsoft.DotNet.SDK.6                 6.0.402                            winget
    3D Viewer                              Microsoft.Microsoft3DViewer_8wekyb3d8  7.2010.15012.0
    Microsoft Sticky Notes                 Microsoft.MicrosoftStickyNotes_8wekyb  3.8.8.0
    Paint 3D                               Microsoft.MSPaint_8wekyb3d8bbwe        6.2009.30067.0
    Microsoft Visual C++ 2015-2019 Redist  Microsoft.VCRedist.2015+.x64           14.28.29325.2           14.34.318  winget
    

    Trying out the Cobalt module that uses Crescendo to parse Winget. There's no name property, and version is just a string (apparently these things are more of a challenge). There's a lot of guid's at the top.

    install-module cobalt -scope currentuser
    get-wingetpackage | sort id
    
    ID                                      Version             Available Source
    --                                      -------             --------- ------
    {04F3299A-F322-45A6-8281-046777B9C736}  21.0.3
    {0E8670B8-3965-4930-ADA6-570348B67153}  11.0.2100.60
    {0EDB70B6-EEA7-413B-BBC4-89E2CD36EFDE}  11.5.18
    #...
    7zip.7zip                               21.07               22.01     winget
    Acrylic Suite
    Acrylic Wi-Fi Home