Reputation: 14897
I'm writing a powershell script that will install some dependencies for my webapp. In my script, I'm running into a recurring problem of checking if a particular application is installed. it seems there's a unique way of checking if an application exists for each application (ie: by checking the existing of this folder or this file on c:). Is there not a way that i can check if an application is installed by querying a list of installed applications?
Upvotes: 11
Views: 48957
Reputation: 3917
You can use Test-Path
to see if the MSI is installed by looking at the corresponding uninstall key in the registry.
if (Test-Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{9BCA2118-F753-4A1E-BCF3-5A820729965C}') {
Write-Output 'IIS URL Rewrite Module 2 is already installed.'
} else {
Write-Output 'IIS URL Rewrite Module 2 is not yet installed.'
}
You'll have to insert the GUID that corresponds to your MSI. You can find the GUID by browsing the entries under Uninstall, via the registry editor.
Upvotes: 3
Reputation: 76
Have your script scan:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
set-location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
Get-ChildItem | foreach-object { $_.GetValue("DisplayName") }
Upvotes: 4
Reputation: 201812
To get a list of installed applications try:
$r = Get-WmiObject Win32_Product | Where {$_.Name -match 'Microsoft Web Deploy' }
if ($r -ne $null) { ... }
See the docs on Win32_Product for more info.
Upvotes: 13
Reputation: 42063
Here is the code I use sometimes (not too often, so...). See the help comments for details.
<#
.SYNOPSIS
Gets uninstall records from the registry.
.DESCRIPTION
This function returns information similar to the "Add or remove programs"
Windows tool. The function normally works much faster and gets some more
information.
Another way to get installed products is: Get-WmiObject Win32_Product. But
this command is usually slow and it returns only products installed by
Windows Installer.
x64 notes. 32 bit process: this function does not get installed 64 bit
products. 64 bit process: this function gets both 32 and 64 bit products.
#>
function Get-Uninstall
{
# paths: x86 and x64 registry keys are different
if ([IntPtr]::Size -eq 4) {
$path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
else {
$path = @(
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
}
# get all data
Get-ItemProperty $path |
# use only with name and unistall information
.{process{ if ($_.DisplayName -and $_.UninstallString) { $_ } }} |
# select more or less common subset of properties
Select-Object DisplayName, Publisher, InstallDate, DisplayVersion, HelpLink, UninstallString |
# and finally sort by name
Sort-Object DisplayName
}
Get-Uninstall
Upvotes: 15