10

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?

burnt1ce
  • 14,387
  • 33
  • 102
  • 162

5 Answers5

14

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
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
13

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.

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    Be aware that Get-WmiObject Win32_Product may modify the target system if it finds errors in your installer database. It's simple and a really helpful function. But if you are using this for anything more than simply detecting installed apps, be prepared to do more work. –  Sep 03 '14 at 13:38
  • While this works and is nice and clean, it's also kinda slow - it takes ~5 secs on my machine to come back with the answer. – Grilse Apr 06 '21 at 10:11
4

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") }
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
user582244
  • 76
  • 3
1

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.

Grilse
  • 3,491
  • 2
  • 28
  • 35
0

I would recommend trying Get-Package.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222