© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
O. HeaumeUnderstanding Microsoft Intunehttps://doi.org/10.1007/978-1-4842-8850-4_10

10. Uninstall an Application

Owen Heaume1  
(1)
West Sussex, UK
 

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

Listing 10-1 shows a single line of PowerShell that can obtain the application GUIDs for all installed MSIs and Figure 10-1 shows the resulting output of the code execution. The correct GUID can then be used to uninstall the application using MSIEXEC as you learned from the earlier chapter.
Get-wmiobject Win32_Product | Format-Table IdentifyingNumber, Name
Listing 10-1

Obtaining a list of all installed MSI GUIDs

Figure 10-1 shows sample output when running the command shown in Listing 10-1.

A screenshot of the Windows Power Shell displays the identifying number and the name for 8 entries.

Figure 10-1.

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.

Another method, again using PowerShell, searches both the 32-bit and 64-bit registry locations for all installed software, both MSI and Setup.exe, and will display the uninstall string if present. Listing 10-2 shows the code required to do this.
$paths=@('HKLM:SOFTWAREWOW6432NodeMicrosoftWindows↩
CurrentVersionUninstall', 'HKLM:SOFTWAREMicrosoft↩WindowsCurrentVersionUninstall')
foreach($path in $paths){
    Get-ChildItem -Path $path | Get-ItemProperty | ↩
Select DisplayName, Publisher, InstallDate, ↩
DisplayVersion, UninstallString
}
Listing 10-2

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.

Figure 10-2 demonstrates all these points – after running the PowerShell shown in Listing 10-2, the output displays Java 8 with the correct MSIEXEC uninstall command line, a Microsoft Visual C++ 2012 Redistributable (x86) setup.exe missing the silent switch and a Microsoft Visual C++ 2019 x86 Minimum Runtime MSI with the incorrect uninstall parameter.

A screenshot of the Windows Power Shell displays the display name, publisher, installs date, display version, and uninstall string for 4 entries.

Figure 10-2

Example uninstall command lines. You may still need to make slight modifications to ensure they work

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.

Example code on how to achieve this can be seen in Listing 10-3.
$VersionNumber = '8.43'
$Executable = "notepad++.exe"
$Path = "${env:ProgramFiles} otepad++"
If ((Get-item (Join-Path -Path $Path -ChildPath $Executable)↩
-ErrorAction SilentlyContinue).VersionInfo.ProductVersion ↩
-eq $VersionNumber) {
    Start-Process -FilePath "$env:ProgramFiles otepad++uninstall.exe" -ArgumentList ↩
"/S" -NoNewWindow -Wait
}
Listing 10-3

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

If you are using the example deployment template from Chapter 8, then you should place this code in the Invoke-PreInstallation function, and this is shown in Figure 10-3.

A screenshot of the invoice application page highlights the code for function invoke preinstallation.

Figure 10-3

Place your uninstall code in the Invoke-PreInstallation function

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.40.53