windows - 检查是否安装了特定版本的程序

标签 windows powershell scripting installed-applications

我正在尝试创建一个非常简单的脚本来检查是否安装了特定程序,如果安装了则返回该程序的版本号。

我已经能够运行脚本并能够返回二进制值(无论是否安装了程序),但不确定如何返回已安装程序的版本号。

我将发布的内容将是我正在做的事情,以便在安装程序时返回,并且在获得版本号时需要帮助。

function Check_Program_Installed {
    $my_check = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
                Select-Object DisplayName, DisplayVersion, InstallDate |
                Format-Table -AutoSize |
                Out-String

    # Check if Google Chrome is installed
    $my_check -Match "Google Chrome"
}

Check_Program_Installed

enter image description here

最佳答案

如果您希望该函数查找特定的已安装程序而不是返回(表格)格式的字符串,那么您可以简单地执行以下操作:

function Check_Program_Installed {
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory=$true, ValueFromPipeline = $true)]
        $Name
    )
    $app = Get-ItemProperty -Path "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | 
                Where-Object { $_.DisplayName -match $Name } | 
                Select-Object DisplayName, DisplayVersion, InstallDate, Version
    if ($app) {
        return $app.DisplayVersion
    }
}

Check_Program_Installed "Google Chrome"

这将在未找到时返回 $null,或者像 70.0.3538.67 这样的字符串版本

关于windows - 检查是否安装了特定版本的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52856487/

相关文章:

python - Windows 10.0.105 Pro 上的 Pip 问题

python - OpenCV aruco 不能在 Python 中工作(Windows)

windows - 神秘的 native "A"注册表项,路径为 : Registry\A

json - 如何修复失败的 VMSS 部署并出现错误 "unknown network allocation error"

bash - 变量值在子 shell 中丢失

shell - 在文件中搜索多个模式并在模式匹配时删除行

linux - 获取脚本并在 KornShell 上执行脚本时的行为差异

c - "Ex"在 Windows API 函数名中代表什么?

.net - 如何查询DOTNET_CLI_TELEMETRY_OPTOUT是否永久设置为TRUE?

.net - 使用 PowerShell 类调用 "[namespace.class]::method"样式命令