You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Winget获取已安装应用版本并存入PowerShell变量

使用PowerShell通过Winget提取已安装应用版本

方法一:通过JSON结构化输出提取(推荐)

利用Winget的--json参数输出结构化数据,避免解析纯文本的格式问题,兼容性更稳定:

# 定义目标应用名称
$appName = "7-Zip"

# 调用Winget获取应用信息并转为PowerShell对象
$appInfo = winget list --name $appName --json | ConvertFrom-Json

# 提取第一个匹配应用的版本号
$appVersion = $appInfo.Version | Select-Object -First 1

# 输出结果(处理应用未安装的情况)
if ($appVersion) {
    Write-Output "The current installed version of $appName is $appVersion"
} else {
    Write-Output "$appName is not installed on this device."
}

方法二:解析纯文本输出(适配旧版Winget)

如果你的Winget版本不支持--json参数,可以通过正则表达式提取版本号:

$appName = "7-Zip"

# 获取Winget的文本输出内容
$wingetOutput = winget list --name $appName

# 用正则匹配常见格式的版本号(如x.x / x.x.x / x.x.x.x)
$versionMatch = $wingetOutput | Select-String -Pattern '(\d+\.\d+|\d+\.\d+\.\d+|\d+\.\d+\.\d+\.\d+)' -AllMatches

# 提取并输出结果
if ($versionMatch.Matches.Success) {
    $appVersion = $versionMatch.Matches.Value | Select-Object -First 1
    Write-Output "The current installed version of $appName is $appVersion"
} else {
    Write-Output "$appName is not installed on this device."
}

内容的提问来源于stack exchange,提问作者GermanKiwi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:40:52