8

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.

1
  • 2
    Have you looked at Chocolatey chocolatey.org? Packages are easy to make and all the hard work is done for you. Commented Jul 30, 2015 at 3:23

2 Answers 2

34
$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
}
Sign up to request clarification or add additional context in comments.

7 Comments

You're welcome. Can you then please mark it as the answer?(it's the tick under the down arrow).
I found this very useful, would you mind explaining what the code: If ([IntPtr]::Size -eq 4) is checking for? Thank you!
@Bajan it's a method to determine the OS architecture. The value of the [IntPtr] property is 4 in a 32-bit process, and 8 in a 64-bit process.
@ChrisVermeijlen; $msiArgs is just a variable to store the arguments you want to pass onto the MSI, in the example above we are just passing "-qb". You could store any other MSI argument like, "/norestart ALLUSERS=2" etc. The "-wait -Passthru" are parameters for the Start-Process cmdlet: learn.microsoft.com/en-us/powershell/module/…
@Tom Padilla - Yes, if the script is on its own, you don't need it, but the OP had it in their original code to pipe out the ExitCode.
|
-2

Example for az command line tool

try {
    az --version
    echo "az is installed"
} catch {
    echo "az isn't installed"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.