You may need to uninstall an application as part of the existing deployment. In this chapter, you will delve a little deeper and learn how to remove an existing application before deploying a new one.
If you recall, in Chapter 2 you learned where in the registry you can find the uninstall GUIDS or commands. It can be cumbersome to find the information by manually browsing the registry, and PowerShell can help make this task a little easier.
PowerShell to the Rescue
Obtaining a list of all installed MSI GUIDs
The drawback to this method is that it can take a long time to populate the list (be patient) and it only retrieves MSI-installed applications.
Searching the registry for uninstall information for both MSI and Setup.exe
When running this PowerShell code and examining the results you will notice some oddities. The MSI uninstall GUIDS are sometimes shown using the /i parameter and this signifies an installation. You will need to change this to /x to signify an uninstallation in your PowerShell code. Other MSI uninstall GUIDS may display the correct parameter.
Additionally, a lot of the setup.exe uninstall command lines are missing the switch for silent removal. Experience has taught me that generally, you will need to add /S at the end of the command for this, but make sure you test it first to ensure it works as expected. If not, use the tactics previously discussed in Chapter 2 to discover the silent switch.
In Practice
In an ideal world, before installing a new application, you should first discover if there is a previous version of the same application already installed. You can do this using a slightly modified detection rule. Then, if the previous version is detected, you would add the command to uninstall it.
All this would happen before the new application is installed and if you are using the example template that you learned how to use in Chapter 8, then this code would go in the Invoke-PreInstallation function.
Detecting the Old Application
Let us say that you must deploy Notepad++, version 8.4.4. You know that some systems may have an older Notepad++, version 8.4.3 installed, and this must be removed first. To do this you will use your existing application detection rule to detect the old version of the application and if found, uninstall it.
You must make one minor modification in your existing PowerShell detection rule for Notepad++. In a normal detection rule, you would use Write-Host "Installed!" to signify a successful detection of the installed application. Instead, you will substitute Write-Host with a modified install command you have already learned about in Chapter 6. Rather than installing an application though, you will uninstall it.
The detection and uninstallation code for removing an older version (version 8.43) of Notepad++. (Note the Write-Host cmdlet has been replaced by Start-Process)
Adding to the Template
Summary
In this chapter, you learned how to use PowerShell to easily obtain the application GUIDS or command lines that can be used to uninstall an application from a system.
You learned how to modify existing PowerShell detection rules to aid with the uninstallation process, and where to place the modified detection rule in the deployment template.
In the next chapter, you will see various examples of short code snippets that can be added to pre- or post-installation functions that will assist you in complex deployments.