-
-
Save JoeyNice/507d2beaf5abe5d0d9f6bb7edc65d61a 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 IDs to skip the update | |
$skipUpdate = @( | |
'Microsoft.DotNet.SDK.6', | |
'Microsoft.WindowsSDK', | |
'Tonec.InternetDownloadManager' | |
) | |
# Define a class to represent software information | |
class Software { | |
[string]$Name | |
[string]$Id | |
[string]$Version | |
[string]$AvailableVersion | |
} | |
# Get the list of available upgrades using winget | |
$upgradeResult = winget upgrade --include-unknown | |
# 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 | |
# Process the output from winget to extract software details | |
$upgrades = @() # Initialize an empty array to hold software objects | |
$idStart = -1 # Variable to determine the start position of ID in the output line | |
$isStartList = 0 # Flag to determine when to start processing lines | |
$upgradeResult | ForEach-Object -Process { | |
# Skip irrelevant lines until the header is found | |
if ($isStartList -lt 1 -and -not $_.StartsWith("Name") -or $_.StartsWith("---") -or $_.StartsWith("The following packages")) { | |
return | |
} | |
# Identify the header line and set the starting position of the ID column | |
if ($_.StartsWith("Name")) { | |
$idStart = $_.toLower().IndexOf("id") # Find the starting position of the ID column | |
$isStartList = 1 # Mark that we have found the header and can start processing | |
return | |
} | |
# Skip lines that are too short to contain valid data | |
if ($_.Length -lt $idStart) { | |
return | |
} | |
# Create a new software object and extract information | |
$Software = [Software]::new() | |
$Software.Name = $_.Substring(0, $idStart - 1).Trim() # Extract the name from the line | |
$info = $_.Substring($idStart) -split '\s+' # Split the rest of the line by whitespace | |
$Software.Id = $info[0] # Extract the ID | |
$Software.Version = $info[1] # Extract the current version | |
$Software.AvailableVersion = $info[2] # Extract the available version | |
# Add the software object to the upgrades list | |
$upgrades += $Software | |
} | |
# Display the list of upgrades in a table format | |
$upgrades | Format-Table | |
# Iterate through the list of upgrades and execute them if they are not in the skip list | |
$upgrades | ForEach-Object -Process { | |
# Check if the current software ID is in the skip list | |
if ($skipUpdate -contains $_.Id) { | |
Write-Host "Skipped upgrade to package $($_.Id)" | |
return | |
} | |
# Upgrade the software package | |
Write-Host "Going to upgrade $($_.Id)" | |
winget upgrade --id $_.Id --include-unknown --accept-package-agreements --accept-source-agreements --silent | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment