Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a program is installed and install it if it is not?

I would rather not use WMI due the integrity check.

This is what I have that does not work:

$tempdir = Get-Location
$tempdir = $tempdir.tostring()

$reg32 = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$reg64 = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"

if((Get-ItemProperty $reg32 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null) -Or (Get-ItemProperty $reg64 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null))
        {
        (Start-Process -FilePath $tempdir"\microsoft.interopformsredist.msi" -ArgumentList "-qb" -Wait -Passthru).ExitCode
        }

It always returns false. If I switch it to -ne $null it always returns true so I know it is detecting $null output even though, I believe (but could be wrong), the Get-ItemProperty is returning a result that should be counting as something other than $null.

like image 445
Joshua Fletcher Avatar asked Jul 29 '15 23:07

Joshua Fletcher


People also ask

How do you check software is installed or not?

Select Start > Settings > Apps. Apps can also be found on Start . The most used apps are at the top, followed by an alphabetical list.

How do I find the install program installed files?

Right-click the “Start” menu shortcut for the application, and select More > Open file location. This will open a File Explorer window that points to the actual application shortcut file.

How does a program know it was installed before?

When you uninstall a program, it often leaves those registry settings in place. Come back some time later to install the program again. The install program finds those registry settings still on your system and states, "This program has previously been installed" or "The trial period for the application has ended".


1 Answers

$tempdir = Get-Location
$tempdir = $tempdir.tostring()
$appToMatch = '*Microsoft Interop Forms*'
$msiFile = $tempdir+"\microsoft.interopformsredist.msi"
$msiArgs = "-qb"

function Get-InstalledApps
{
    if ([IntPtr]::Size -eq 4) {
        $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    }
    else {
        $regpath = @(
            'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
            'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
        )
    }
    Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString |Sort DisplayName
}

$result = Get-InstalledApps | where {$_.DisplayName -like $appToMatch}

If ($result -eq $null) {
    (Start-Process -FilePath $msiFile -ArgumentList $msiArgs -Wait -Passthru).ExitCode
}
like image 169
Jez Avatar answered Nov 10 '22 04:11

Jez