- Feb 26, 2021
- 965
There is another thread here about application updaters. One of the apps mentioned there was a somewhat-os-integrated updater called winget, which can be run via script for automation purposes. Winget is amazing by itself, because it can keep your apps updated silently. Not every application is listed, but a lot of popular software is listed.; at the time of this post there are 7111 packages in the repo. However it does have one flaw that I can see; users could not exclude software from being updated. In my case, I didn't want MS Edge being updated/reinstalled because I won't use it. I've come up with a powershell script to change that.
Edit the colored exclusion entries to fit your needs. Also, you may want to consider creating a bat file, like the one below. That way the execution policy of powershell scripts will not be a problem. Change "scriptlocationandname" in the example below to the location and given name of the above powershell script.
Anyways, that's it. I hope you find it useful.
Code:
class Software {
[string]$Name
[string]$Id
[string]$Version
[string]$AvailableVersion
}
$upgradeResult = winget upgrade | Out-String
$lines = $upgradeResult.Split([Environment]::NewLine)
# Find the line that starts with Name, it contains the header
$fl = 0
while (-not $lines[$fl].StartsWith("Name"))
{
$fl++
}
# Line $i has the header, we can find char where we find ID and Version
$idStart = $lines[$fl].IndexOf("Id")
$versionStart = $lines[$fl].IndexOf("Version")
$availableStart = $lines[$fl].IndexOf("Available")
$sourceStart = $lines[$fl].IndexOf("Source")
# Now cycle in real package and split accordingly
$upgradeList = @()
For ($i = $fl + 1; $i -le $lines.Length; $i++)
{
$line = $lines[$i]
if ($line.Length -gt ($availableStart + 1) -and -not $line.StartsWith('-'))
{
$name = $line.Substring(0, $idStart).TrimEnd()
$id = $line.Substring($idStart, $versionStart - $idStart).TrimEnd()
$version = $line.Substring($versionStart, $availableStart - $versionStart).TrimEnd()
$available = $line.Substring($availableStart, $sourceStart - $availableStart).TrimEnd()
$software = [Software]::new()
$software.Name = $name;
$software.Id = $id;
$software.Version = $version
$software.AvailableVersion = $available;
$upgradeList += $software
}
}
$upgradeList | Format-Table
$toSkip = @(
'Microsoft.EdgeWebView2Runtime',
'Microsoft.Edge',
"Microsoft.OneDrive")
foreach ($package in $upgradeList)
{
if (-not ($toSkip -contains $package.Id))
{
Write-Host "Going to upgrade package $($package.id)"
& winget upgrade $package.id
}
else
{
Write-Host "Skipped upgrade to package $($package.id)"
}
}
Edit the colored exclusion entries to fit your needs. Also, you may want to consider creating a bat file, like the one below. That way the execution policy of powershell scripts will not be a problem. Change "scriptlocationandname" in the example below to the location and given name of the above powershell script.
Code:
@echo off
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\scriptlocationandname.ps1'"
TIMEOUT /T 5
Anyways, that's it. I hope you find it useful.
Last edited: