If statement to check for an installed application then uninstall if found.

Topics: Archive - General
Sep 30, 2014 at 2:02 PM
I am trying to look at some of the examples and code to figure out how I can first do a check during the pre-installation stage to see if an app is installed, and if so to uninstall it.

Then I need to do an uninstall of the existing program, then perform an install of the "new" program.

Originally when the software was deployed it was supposed to uninstall the existing software, but it didn't so users now have to versions of this outlook plugin. Now they want a new version deployed, so I figured I would take this opportunity to do a check for the original and remove, then remove the existing and then install the latest version.

I am not very well versed in powershell and still learning.

The original file is an MSI so I know I can call it to uninstall, I just don't know how to do the check to see if it exists first, and if not to move on to the second uninstall string.
Oct 1, 2014 at 1:41 PM
This is what I have done for Jave and Adobe in the past. Java Example below. Because you are uninstalling an application that was installed via MSI, this should work with your application:
Show-InstallationProgress "Removing old Versions of Java..."
Get-Process -Name "jusched.exe"  -EA SilentlyContinue | Stop-Process -Force -EA SilentlyContinue
$Java7Apps = Get-WmiObject -query "select * from win32_Product where name like `"Java 7 %`""
If ($Java7Apps -is [System.Array]) {
    foreach ($Java in $Java7Apps) {$Java.Uninstall()}
elseIf ($Java7Apps) {$Java7Apps.Uninstall()}
The above code first finds the jusched.exe process. If it is running it kills it. This is a java process that can effect the install of the new version. I include this because your app could have something similar.

Then it searches WMI for any application that is installed with a name that starts with "Java 7 ". It assigns those object(s) (applications) to a variable.

The IF then checks if it is an array. If it is, then there are more than 1 version of this application installed. In the case of Java, it could be 32 and 64 bit. It then loops through all of the items in the array and uninstalls them. If it is not an array, the ELSEIF will check if the variable exists. If it does, then it removes the single version of java in the variable. If not, the install continues without uninstalling.

So, what you would need to test with your application is the following
Get-WmiObject -query "select * from win32_Product where name like `"App Name %`""
When you get a result of your application only, you should be able to insert that into the script.

This would go into the PRE-INSTALLATION section of the Deploy-Application.ps1 script.

Hope this helps.
Oct 1, 2014 at 1:48 PM
There is also a Remove-MSIApplications function in the toolkit that may work for you. See the documentation for additional help with that. I have not used it so I can't offer my experiences.
Oct 1, 2014 at 2:41 PM
Thanks for the help Paul.

I will test it and report back.

I did try the Remove-MSIApplications, but for whatever reason it will not uninstall this application.

I then decided to build an uninstaller using PsApp with the following code:

Execute-MSI -Action Uninstall -Path "{49E48BAD-ED29-44FF-BB27-9BE66CC61852}"

and it works. I want to think it is because the old version of this app is installed in the "users app data folder", but the new version installs in ProgramData, but this should not matter as I do reference the GUID.

Maybe I am better off running the Execute-MSI -Action Uninstall -Path "{49E48BAD-ED29-44FF-BB27-9BE66CC61852}" then add a pause, then remove the second application.

You have given me something to think about regardless.