Skip to content

Instantly share code, notes, and snippets.

@nouseforname
Last active October 14, 2024 01:15
Show Gist options
  • Save nouseforname/a8b7ebcb9d0c05e380c7e3c81c300923 to your computer and use it in GitHub Desktop.
Save nouseforname/a8b7ebcb9d0c05e380c7e3c81c300923 to your computer and use it in GitHub Desktop.
Powershell filter winget upgrade
# add id to skip the update
$skipUpdate = @(
'Microsoft.DotNet.SDK.6',
'Microsoft.WindowsSDK'
)
# object to be used basically for view only
class Software {
[string]$Name
[string]$Id
[string]$Version
[string]$AvailableVersion
}
# get the available upgrades
$upgradeResult = winget upgrade -u
# run through the list and get the app data
$upgrades = @()
$idStart = -1
$isStartList = 0
$upgradeResult | ForEach-Object -Process {
if ($isStartList -lt 1 -and -not $_.StartsWith("Name") -or $_.StartsWith("---") -or $_.StartsWith("The following packages"))
{
return
}
if ($_.StartsWith("Name"))
{
$idStart = $_.toLower().IndexOf("id")
$isStartList = 1
return
}
if ($_.Length -lt $idStart)
{
return
}
$Software = [Software]::new()
$Software.Name = $_.Substring(0, $idStart-1)
$info = $_.Substring($idStart) -split '\s+'
$Software.Id = $info[0]
$Software.Version = $info[1]
$Software.AvailableVersion = $info[2]
$upgrades += $Software
}
# view the list
$upgrades | Format-Table
# run through the list, compare with the skip list and execute the upgrade (could be done in the upper foreach as well)
$upgrades | ForEach-Object -Process {
if ($skipUpdate -contains $_.Id)
{
Write-Host "Skipped upgrade to package $($_.id)"
return
}
Write-Host "Going to upgrade $($_.Id)"
winget upgrade -u $_.Id
}
@ShaguarWKL
Copy link

ShaguarWKL commented Mar 4, 2023

❯ winget-Updates-with-Ignore

Name                                             Id                                Version        AvailableVersion
----                                             --                                -------        ----------------
Miniconda3 py310_23.1.0-1 (Python 3.10.9 64-bit) Anaconda.Miniconda3               py310_23.1.0-1 py39_4.10.3
Microsoft Windows Desktop Runtime - 6.0.10 (x86) Microsoft.DotNet.DesktopRuntime.6 6.0.10         6.0.14
FotoSketcher 3.80                                DavidTHOIRON.FotoSketcher         Unknown        3.80
Microsoft Windows Desktop Runtime - 5.0.10 (x86) Microsoft.DotNet.DesktopRuntime.6 <              6.0.5
The following packages have an upgrade available                                   but            require
Discord                                          Discord.Discord                   1.0.9010       1.0.9011

Skipped upgrade to package Anaconda.Miniconda3
Going to upgrade Microsoft.DotNet.DesktopRuntime.6
Going to upgrade DavidTHOIRON.FotoSketcher
Going to upgrade Microsoft.DotNet.DesktopRuntime.6
Going to upgrade
Skipped upgrade to package Discord.Discord

It looks like it doesn't ignore the line "The following packages have an upgrade available" and adds that empty to Going to Upgrade. It resulted in winget parsing the entire library with nothing to upgrade since its the entire uninstalled list on my end.

PS Totally forgot to say thank you for optimising this script.

@nouseforname
Copy link
Author

nouseforname commented Mar 4, 2023

did u uncomment the last line "winget upgrade -u ..." ?
it was commented out, so no real upgrade was done.

winget seems to be bit unreliable on win11 as well. Also i would ignore those .net6 upgrades, these should be done with windows update anyway and seems to often fail with winget

@HankMardukasNY
Copy link

@ShaguarWKL if you change line 26 to the following you can exclude that:

if ($isStartList -lt 1 -and -not $_.StartsWith("Name") -or $_.StartsWith("---") -or $_.StartsWith("The following packages have an upgrade"))

@nouseforname
Copy link
Author

@ShaguarWKL if you change line 26 to the following you can exclude that:

if ($isStartList -lt 1 -and -not $_.StartsWith("Name") -or $_.StartsWith("---") -or $_.StartsWith("The following packages have an upgrade"))

ah, i misunderstood. I never had that line it seems. Added it. thx

@ShaguarWKL
Copy link

did u uncomment the last line "winget upgrade -u ..." ? it was commented out, so no real upgrade was done.

Yes I did uncomment that's how I saw the empty field ended up listing the entire winget library.

winget seems to be bit unreliable on win11 as well. Also i would ignore those .net6 upgrades, these should be done with windows update anyway and seems to often fail with winget

Yup it says upgrade but never upgrade. It's added to my ignore already. FotoSketcher is another one, the Winget version has 3.80 but for some reason it can't pull the installed version which is also 3.80

@ShaguarWKL
Copy link

@ShaguarWKL if you change line 26 to the following you can exclude that:

if ($isStartList -lt 1 -and -not $_.StartsWith("Name") -or $_.StartsWith("---") -or $_.StartsWith("The following packages have an upgrade"))

Much Obliged.

@igelover
Copy link

Nice work! There's some minor issue with a package though. This is the winget update output:

Name                                                               Id                           Version          Available        Source
----------------------------------------------------------------------------------------------------------------------------------------
Microsoft 365 Apps for enterprise - en-us                          Microsoft.Office             16.0.14931.21024 16.0.16327.20214 winget
Oh My Posh version 17.3.0                                          JanDeDobbeleer.OhMyPosh      17.3.0           17.4.0           winget
Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.28.29914 Microsoft.VCRedist.2015+.x64 14.28.29914.0    14.36.32532.0    winget
DisplayLink Graphics                                               DisplayLink.GraphicsDriver   10.3.6400.0      11.0.2412.0      winget
Teams Machine-Wide Installer                                       Microsoft.Teams              1.4.0.19572      1.6.00.6754      winget
5 upgrades available.

And for some reason the Microsoft Visual C++ package info is not being formatted properly.

Name                                              Id                         Version                      AvailableVersion
----                                              --                         -------                      ----------------
Microsoft 365 Apps for enterprise - en-us         Microsoft.Office           16.0.14931.21024             16.0.16327.20214
Microsoft Visual C++ 2015-2019 Redistributable (Ô ª                          Microsoft.VCRedist.2015+.x64 14.28.29914.0
DisplayLink Graphics                              DisplayLink.GraphicsDriver 10.3.6400.0                  11.0.2412.0
Teams Machine-Wide Installer                      Microsoft.Teams            1.4.0.19572                  1.6.00.6754

Skipped upgrade to package Microsoft.Office
Going to upgrade ª
No installed package found matching input criteria.
Skipped upgrade to package DisplayLink.GraphicsDriver
Skipped upgrade to package Microsoft.Teams

@AsadGG
Copy link

AsadGG commented Sep 4, 2023

i am using this
Start-Process powershell -ArgumentList "winget upgrade -u $($_.Id); Read-Host"
instead of
winget upgrade -u $_.Id

is there a problem with this aproach?

@JoeyNice
Copy link

JoeyNice commented Oct 13, 2024

Nice work! There's some minor issue with a package though. This is the winget update output:

Name                                                               Id                           Version          Available        Source
----------------------------------------------------------------------------------------------------------------------------------------
Microsoft 365 Apps for enterprise - en-us                          Microsoft.Office             16.0.14931.21024 16.0.16327.20214 winget
Oh My Posh version 17.3.0                                          JanDeDobbeleer.OhMyPosh      17.3.0           17.4.0           winget
Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.28.29914 Microsoft.VCRedist.2015+.x64 14.28.29914.0    14.36.32532.0    winget
DisplayLink Graphics                                               DisplayLink.GraphicsDriver   10.3.6400.0      11.0.2412.0      winget
Teams Machine-Wide Installer                                       Microsoft.Teams              1.4.0.19572      1.6.00.6754      winget
5 upgrades available.

And for some reason the Microsoft Visual C++ package info is not being formatted properly.

Name                                              Id                         Version                      AvailableVersion
----                                              --                         -------                      ----------------
Microsoft 365 Apps for enterprise - en-us         Microsoft.Office           16.0.14931.21024             16.0.16327.20214
Microsoft Visual C++ 2015-2019 Redistributable (Ô ª                          Microsoft.VCRedist.2015+.x64 14.28.29914.0
DisplayLink Graphics                              DisplayLink.GraphicsDriver 10.3.6400.0                  11.0.2412.0
Teams Machine-Wide Installer                      Microsoft.Teams            1.4.0.19572                  1.6.00.6754

Skipped upgrade to package Microsoft.Office
Going to upgrade ª
No installed package found matching input criteria.
Skipped upgrade to package DisplayLink.GraphicsDriver
Skipped upgrade to package Microsoft.Teams

The problem is, the text for VisualC++ Name is too long for the column and in my Powershell window it shows me 3 dots (...) at the end, but that's not three dots, it's a special character that looks like 3 dots and that messes up the script.
I assume you are using Visual Studio Code, which also shows me .

Insert after line 17:
# Remove ellipsis character (Unicode 2026) and other potential problematic encodings
$upgradeResult = $upgradeResult -replace [char]0x2026, ' ' # Replace Unicode ellipsis with a space
$upgradeResult = $upgradeResult -replace '', ' ' # Replace other potential ellipsis encodings with a space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment