1

Currently working with WinGet to improve application deployment lifecycle within Intune. I am looking at deploying a detection script that checks for the installed version daily, then triggers the remediation if there is an available update.

Doing something as simple as:

winget list --name 7-Zip

will return as follows:

Name              Id        Version Available Source
----------------------------------------------------
7-Zip 19.00 (x64) 7zip.7zip 19.00   22.01     winget

Within Powershell, is there a way we can check and compare the Available "column" to the Version "column", and return an exit 0 or exit 1?

Thanks in advance

Batteredburrito
  • 579
  • 1
  • 5
  • 34

2 Answers2

1

If all you need to know is whether or not an upgrade is available, to be reflected in the script's exit code:

The following reports exit code 1 if an upgrade is available, and 0 otherwise, relying on an Available column header only being present if an upgrade is available:

exit [int] (winget list --name 7-Zip | Select-String '\bVersion\s+Available\b' -Quiet)

If you also want to report the installed vs. the latest available version, more work is needed:

$name = '7-Zip'
$lines = winget list --name $name
if ($lines -match '\bVersion\s+Available\b') {
  $verinstalled, $verAvailable = (-split $lines[-1])[-3,-2]
  [pscustomobject] @{
    Name = $name
    InstalledVersion = [version] $verInstalled
    AvailableVersion = [version] $verAvailable
  }
  exit 1
} else {
  Write-Verbose -Verbose "No upgrade for $name available."
  exit 0
}

The above outputs something like the following if an upgrade is available; the exit code is set as in the first command:

Name    InstalledVersion AvailableVersion
----    ---------------- ----------------
7-Zip   9.0.30729.4148   9.0.30729.6161

Alternatively, if it's acceptable to blindly try to upgrade:

winget upgrade --name 7-Zip --silent
# If $LASTEXITCODE contains -1978335189, an upgrade was NOT available.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • This works amazingly, I appreciate the support. Only thing thats been noticed is if the application has multiple spaces, such as Adobe Acrobat DC, the Split needs to be amended by the seems of things. Is this a correct observation? – Batteredburrito Aug 10 '22 at 10:00
  • @Batteredburrito, it's fine for the `Name` column value to have spaces. You'd only have a problem if the `Source` column value had spaces. The preinstalled source (`winget`, `msstore`, determined via`winget source list`) do _not_. Do you have / know of sources that do (I'm asking innocently)? – mklement0 Aug 10 '22 at 13:16
  • Got it, thanks so much for the prompt answer, really helped me out on this one. Marking as solved – Batteredburrito Aug 10 '22 at 23:19
  • Glad to hear it, @Batteredburrito; my pleasure. Thanks for accepting. – mklement0 Aug 10 '22 at 23:32
0

Why not use

winget upgrade

it will return available upgrades

like

Name                                                      Id                                    Version          Available     Source
Google Chrome                                             Google.Chrome                         103.0.5060.53    104.0.5112.81 winget
Microsoft Edge                                            Microsoft.Edge                        103.0.1264.49    104.0.1293.47 winget
  • Doing the upgrade command will list all apps on system and their relevant updates. For my use case, individual app updates are checked and remediated which offers more granularity. I am okay with either method, but still need to resolve the exit codes – Batteredburrito Aug 10 '22 at 02:31