Last active
October 14, 2024 01:15
-
-
Save nouseforname/a8b7ebcb9d0c05e380c7e3c81c300923 to your computer and use it in GitHub Desktop.
Powershell filter winget upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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