Home > front end >  How to sort the output of winget list by column in powershell?
How to sort the output of winget list by column in powershell?

Time:11-02

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?

CodePudding user response:

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

CodePudding user response:

Here is my take on the problem to avoid hardcoded position of the ID column. At least on my german-localized system, the column is one char off to the left. Search for the ID word in the header row to determine how much to chop off for sorting.

# Strip two blank lines and split into header string and items array 
$wgHdr, $null, $wgItems = winget list | Select-Object -skip 2

# Get the position of the 'ID' column.
$idPos = [regex]::Match( $wgHdr,'\bID\b' ).Index

# Sort beginning at the position of the 'ID' column
$wgItems | Sort-Object { $_.Substring( $idPos ) }
  • Related