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!