Search code examples
powershellwinget

Return the current version of an app with winget


I'm trying to figure out how I can grab the current version of an app installed on a computer using Winget, and save it to a variable.

Eg. if I enter:

winget list --name 7-Zip

This returns:

Name              Id        Version Source
-------------------------------------------
7-Zip 24.09 (x64) 7zip.7zip 24.09   winget

So I want to grab only the "24.09" part of this output. Unfortunately my knowledge of PowerShell scripting isn't quite advanced enough to do this.

My goal is to create a remediation script for Intune where I can output something like this at the end:

Write-Output "The current installed version of $appName is $appVersion"

Solution

  • To provide an object-based alternative to Theo's helpful text parsing-based answer:

    • There is (now) an official PowerShell module, Microsoft.WinGet.Client, which provides the functionality of the winget.exe CLI in the form of - by definition object-oriented - *-WinGet* cmdlets. You can install it by running, e.g.,
      Install-Module -Scope CurrentUser Microsoft.WinGet.Client

    • With this module installed, the solution can be simplified to the following:

       # ->, e.g.: '24.09'
       (Get-WinGetPackage -Name 7-Zip).InstalledVersion