Using Powershell to Uninstall Applications

Automating to uninstall software from a Windows machine can sometimes be a cumbersome task, especially to uninstall applications based on the versions installed. In such cases, PowerShell can come to the rescue by leveraging the Windows Registry to find and uninstall the application in question. Today, I’ll share a script that will uninstall almost any type of application, which can be used along with SCCM or MDM solutions as well.

What the script will do?

This script will query the registry for installed programs and start the uninstallation process. The script should be run in administrative mode.

For programs installed in user context, I’ll later write another post about the script that use winget for uninstallation.

Features

  • Uninstall the program with version lower than specified version
  • Uninstall the program for any version installed
  • Stop the process before program uninstallation (it can lose any unsaved work of the user, beware)
  • Uninstall using custom parameters (rather than using uninstallation parameter defined in registry)

Variables in the Script

$SelfDelete – Delete the script after uninstallation. Valid Values are $True or $False.

$SelfDeleteFilePath -Specifies the full path of the script to enable self-deletion after execution. The script waits for 60 seconds after execution is complete. This setting only takes effect when used with the $SelfDelete variable

$ProgramName – The program name found in Program and Features dialogue of Control Panel. If this value matches two or more programs, there’s a risk of uninstalling all program that are matched by the name. In that case, you’d need to define the program name as much exactly as you can.

Eg: If you have Adobe Acrobat Reader and Foxit PDF Reader installed. Using Reader in the $ProgramName will install both programs.

$NoUserPrompt – Prompt the user with confirmation to continue uninstallation. Valid values are $True or $False.

$MinimumVersion – The minimum version to keep the software, all versions of the software under this version will be uninstalled. This version format needs to contain at least one dot and digit after the major version number. The example valid patterns are: 1.0 or 2.0.1 or 3.1.0.1. This setting only takes effect when $Remove_Apps_WithoutVersion is set to $False.

$CustomParameters – The parameter to be used along with the uninstaller executable file. Eg: “–uninstall –system-level –force-uninstall” for chrome uninstallation. If this variable is set (non-empty), then any parameter found in the registry is ignored.

$RemoveUnMatchedVersions – This variable need to set to $True to actually uninstall the software. Use $False for dry run.

$Remove_Apps_WithoutVersion – Setting it $True will remove the software even if the version registry key is present or not, meaning that it will remove the software regardless of the version installed. You can set it to $False if you want to make a version-check before software uninstallation.

$StopProcess – Terminates the process (defined in $ProcessListToStop) before uninstallation. Using this parameter may cause the user to lose any unsaved work in the program. Valid values are $True or $False.

$ProcessListToStop – Name of the processes to stop. This setting only takes effect when $StopProcess variable is set to $True. You can define muliple processes in array. Eg: “Firefox”,”Chrome”,”7zFM”

Download

You can download the script from gist.

Example Screenshots

Here are the sample screenshots for 7-Zip File Manager uninstallation. Here we have two 7-zip versions.

Fig-1: 7-Zip File Manager

We will make the user prompt before uninstallation to see what programs will actually be uninstalled. Here are the variables defined in the script for this scenario.

VariableValue
$SelfDelete$False
$SelfDeleteFilePath“C:\Temp\Uninstall_Specific_Program_v1.5.ps1”
$ProgramName“7-Zip”
$NoUserPrompt$False
$MinimumVersion24.0
$CustomParameters“”
$RemoveUnMatchedVersions$True
$Remove_Apps_WithoutVersion$False
$StopProcess$True
$ProcessListToStop“7zFM”
Table-1: Variables used to uninstall 7-zip with version lower than 24.0

When we run the script, it prompts to confirm for the removal of version 16.4 but it skips version 24.7.

Fig-2: Run with User Prompt

Now, we changed the $NoUserPrompt to $True to uninstall the version 16.4 without user interruption. See Fig-3.

Fig-3: Run without User Prompt

Below is the code, you can download the script from gist as well.

Leave a Reply

Your email address will not be published. Required fields are marked *