PowerShell snippet: check if software is installed

Some deployment scripts need to check if certain required software is installed on a Windows Machine. You could check if a specific file is present at a certain location, but there is a better way: the uninstall database in the Windows Registry!

PowerShell makes it really easy to query the registry using Get-ItemProperty. The path you want to query is HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*.

Example: is .Net Core 3.1 Runtime installed?

Let's check:

$software = "Microsoft .NET Core Runtime - 3.1.0 (x64)";
$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $software }) -ne $null

If(-Not $installed) {
	Write-Host "'$software' NOT is installed.";
} else {
	Write-Host "'$software' is installed."
}

The script is both readable and easy to understand.

Need something smaller?

If you really want to hurt yourself need something smaller, check this one-liner:

(gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Contains "Microsoft .NET Core Runtime - 3.1.0 (x64)"

Need to check a partial name?

If you need to match a partial name, you can use the -Match option. But be careful: you might match more then a single installation! So we need to evaluate the result differently and do a -gt 0 to see if there are results.

((gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Match "Core Runtime - 3.1").Length -gt 0

Need to know the name of software installed? Use the same query to print matching results:

(gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Match "Core SDK"

PowerShell for the win!

  1. Phatmandrake says:

    1-2-3-4 I declare a 1-line war!

    (gp HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall*).displayname -contains "Microsoft .NET Core Runtime - 2.0.0 (x64)"
    
    
    
    1. Kees C. Bakker says:

      Sweet! Added it to the blog. Looks more elegant!

  2. phoenix says:

    I am looking to do something similar to this but there are possibly multiple version/names of the software IE Microsoft Visual C++ 2015 *. How would I be able to check for an abbreviated name? Also Bonus is there a way to check if software is installed and if not to install.

    1. Kees C. Bakker says:

      You can check on partial names with a slightly different Powershell command:
      ((gp HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall*).DisplayName -Match "Core Runtime - 2.1.0").Length -gt 0

  3. Michael C. Cook says:

    an even shorter and more effective method …
    [email protected](gp “HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall*”).DisplayName
    if ($Check -contains “Target”) { Do stuff }

    This method returns the keys under the registry path. Pipelining is great for a lot of things, but when you’re trying to check if a program is installed, and you have an array that you want to pipe through this, it’s better to replace “Target” with another array variable that you can state before the $Check command.

    I actually truncated and added this function to a do loop and it works great.

  4. sameer says:

    Hi All

    I need a PowerShell script that should look for the NET Core 3.1 SDK installed in the system, if not, installed it
    Please help at the earliest

    1. Kees C. Bakker says:

      To query your programs just use: (gp HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall*).DisplayName -Match “Core SDK 3.1”

expand_less