Check if a Program Is Installed Using PowerShell 3

When I write scripts to automate the deployment of software, I always prefer to check to see if a program is already installed rather than run the install and see if it fails.  One way to do this is to use Get-WmiObject to query the WMI_Product table. This is certainly the most direct way but it’s slow and Microsoft recommends against it, as it forces the Windows Installer service to check the validity of all installed MSI’s and can actually cause problems.

Microsoft’s solution is to query the HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall registry key to check for installed software. The code at the link I provided works, but since PowerShell provides direct access to the registry with Get-ChildItem there’s a far easier way to do it:

<# Returns true if a program with the specified display name is installed. This function will check both the regular Uninstall location as well as the "Wow6432Node" location to ensure that both 32-bit and 64-bit locations are checked for software installations. @param String $program The name of the program to check for. @return Booleam Returns true if a program matching the specified name is installed. #> function Is-Installed( $program ) { $x86 = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") | Where-Object { $_.GetValue( "DisplayName" ) -like "*$program*" } ).Length -gt 0; $x64 = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") | Where-Object { $_.GetValue( "DisplayName" ) -like "*$program*" } ).Length -gt 0; return $x86 -or $x64; }

Update: I noticed that the original version of the code was not returning true for 64-bit software that I knew to be installed.  I updated the function to check the “Wow6432Node” in the registry as well.