Check If a Program Is Installed

Detecting if a program is installed and where using cmd.exe

Take a look at this registry key.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

In it you will find something similar to this for firefox.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe]
@="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"
"Path"="C:\\Program Files (x86)\\Mozilla Firefox"

and here is a bit that reads the value from Powershell. Same thing can be done from batch file with reg.exe.

$Firepath = get-item -path 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe'
$Firepath.GetValue('')

Check if program with specific version is installed

If you want that function to look for a specific installed program instead of returning a (table) formatted string, then you could simply do:

function Check_Program_Installed {
[CmdletBinding()]
Param(
[Parameter(Position = 0, Mandatory=$true, ValueFromPipeline = $true)]
$Name
)
$app = Get-ItemProperty -Path "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" |
Where-Object { $_.DisplayName -match $Name } |
Select-Object DisplayName, DisplayVersion, InstallDate, Version
if ($app) {
return $app.DisplayVersion
}
}

Check_Program_Installed "Google Chrome"

This will return $null when not found, or the version as string like 70.0.3538.67

Check if program is installed if so go to next powershell

If you know the GUID, you could test path the uninstall key. Also don't forget that if your OS is 64 bit, there will be the same key in WOW6432Node for 32 bit apps.

$uninstallkey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\"
$uninstall32key = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
#Example 64-bit app
$app1guid = "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
if (!(Test-Path "$uninstallkey\$app1guid)) {Execute-MSI -Action Install -Path "$dirFiles\Program1"}
#Example 32-bit app
$app2guid = "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
if (!(Test-Path "$uninstall32key\$app2guid)) {Execute-MSI -Action Install -Path "$dirFiles\Program1"}

Check if application is installed in registry

After searching and troubleshooting, I got it to work this way:

public static bool checkInstalled (string c_name)
{
string displayName;

string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(c_name))
{
return true;
}
}
key.Close();
}

registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(c_name))
{
return true;
}
}
key.Close();
}
return false;
}

And I simply just call it using

if(checkInstalled("Application Name"))

Is it possible to check if program is already running before trying to install it? (Inno Setup)

If it is your application, make it create a mutex. Then you can use AppMutex directive.

[Setup]
AppMutex=MyProgMutex

Sample Image


If you cannot modify the application, you need to code the check for running application in Inno Setup. You can for example use IsAppRunning function from the answer by @RRUZ to How to check with Inno Setup, if a process is running at a Windows 2008 R2 64bit? in InitializeSetup event function.

function InitializeSetup(): Boolean;
var
Answer: Integer;
begin
Result := True;
while IsAppRunning('MyProg.exe') do
begin
Answer := MsgBox('Program is running, please close it', mbError, MB_OKCANCEL);
if Answer = IDCANCEL then
begin
Result := False
Exit;
end;
end;
end;

Based on a similar question on uninstaller:

Preparing to Uninstall like Preparing to Install Page - Inno Setup



Related Topics



Leave a reply



Submit