最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - Return the current version of an app with winget - Stack Overflow

programmeradmin0浏览0评论

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"

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"
Share Improve this question asked Feb 5 at 13:06 GermanKiwiGermanKiwi 476 bronze badges 2
  • (winget list --name 7-Zip).Version ? – Paolo Commented Feb 5 at 13:37
  • @Paolo: PowerShell interprets an external program's (such as winget.exe's) stdout output as lines of text (strings), and strings have no properties (other than the text they represent and a .Length property); see this answer for background information. – mklement0 Commented Feb 5 at 16:47
Add a comment  | 

2 Answers 2

Reset to default 3

Winget list returns a string array with progress-display lines before the (fixed-width) table with the info you want.

Credit where credit is due, I found an excellent answer by mklement0 from which I have taken the regex to remove (most of) these progress lines.

In your case, you can get the version from the output like below:

$appName    = '7-zip'
$appVersion = switch -Regex ((winget list --name $appName) -match '^\p{L}|-') {
    '^Name\s+' {
        # from the header get the starting position of 'Version'
        $startIndex = $_.IndexOf('Version')  
    }
    "^$appName" {
        if ( $_.Substring($startIndex) -match '^([0-9.]+).*$') {
            $matches[1]
            break
        }
    }
}

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

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
    
发布评论

评论列表(0)

  1. 暂无评论