Winget app Updater Exclusion Script

n8chavez

Level 20
Thread author
Well-known
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.

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:

n8chavez

Level 20
Thread author
Well-known
Feb 26, 2021
965
I apologize, it seems that I cannot change the color of any of the text. I did so several times, yet after I saved the changes no color was visible. The colored exclusion entries I am referring to in the script include the below section.

Code:
'Microsoft.EdgeWebView2Runtime',
'Microsoft.Edge',
"Microsoft.OneDrive")
 
  • Like
Reactions: SeriousHoax

SeriousHoax

Level 49
Verified
Top Poster
Well-known
Mar 16, 2019
3,864
Thanks for sharing. Do you have a GitHub account where you can share it? It would be easier that way to bookmark/fork it for future use.
 

n8chavez

Level 20
Thread author
Well-known
Feb 26, 2021
965
Thanks for sharing. Do you have a GitHub account where you can share it? It would be easier that way to bookmark/fork it for future use.

I don't. I've never needed one before. I guess I never felt nerdy enough for one, and I use that term aspirationally.
 
  • Like
Reactions: SeriousHoax

About us

  • MalwareTips is a community-driven platform providing the latest information and resources on malware and cyber threats. Our team of experienced professionals and passionate volunteers work to keep the internet safe and secure. We provide accurate, up-to-date information and strive to build a strong and supportive community dedicated to cybersecurity.

User Menu

Follow us

Follow us on Facebook or Twitter to know first about the latest cybersecurity incidents and malware threats.

Top