Using PowerShell to Uninstall Applications
Part of my Monitor Information Reporting (MIR) testing (MIR is now part of Enhansoft Reporting) involved deploying several updates to my ConfigMgr lab’s clients. My lab’s a mix of both x86 and x64 operating systems (OS). After deploying all of these versions of MIR, I was left looking for a solution that would enable me to quickly clean-up the client installs. Ultimately I only wanted the final version installed on each client.
Unfortunately there was no way of knowing which versions of MIR were deployed to which test computers. There was also the possibility that several test computers could have more than one version of MIR installed. I needed a way to automatically uninstall MIR without having to manually uninstall it on each computer.
I wanted a script that would detect all versions of MIR on a computer and uninstall each one, one at a time. You would think that a simple solution already existed! Have ever deployed Adobe Reader? Then you would know that there can be several different versions of Adobe Reader listed within Add/Remove Program (ARP) on a computer. Why wouldn’t there be an easy way to delete the older versions?
After searching the net for a while, I found a lot of scripts that would help me uninstall only one application at a time. There were even a few scripts out there that would query Win32_Product and then uninstall two or more applications. However, if you have ever read Greg Ramsey’s blog post Win32_Product Is Evil then you will know that this is a bad idea. You should never do this! That’s why I could never use Win32_Product within my script due to the problems it creates.
In the end, I pieced together a script that at a high level:
· Detects if the script is running as x84, x64 or WoW32 mode.
· Reads both x86 and x64 uninstall registry keys.
· Locates all versions of an application.
· Looping through the list of ARP entries it collects the:
o MSI name/GUID.
o Uninstall details on each MSI.
· Logs each step to a log file.
What does the final script look like? See the screenshot below. You can also download it from the Enhansoft website.
How can you deploy this script with ConfigMgr?
First, start by editing the script. Replace the application’s name with the one you want to uninstall. In the download version it is currently set to:
Remember that the * is a wild card used in searching ARP entries.
Second, create a package and program for your command line. It should look similar to the following:
powershell.exe -ExecutionPolicy bypass -file MIR_Uninstallv9.ps1
Third, deploy the package and program as you would any other program.
Next I’ll show you what I did on one of my test computers. You can see in the screenshot below that I had several copies of MIR installed.
I setup my program to silently deploy in the background. Nothing to see from an end-user’s perspective!
However, I can verify that the PowerShell script is running by looking at the Task Manager. You’ll have to be quick if you’d like to do this too! The script only takes a few seconds to a few minutes to run depending on the number of ARP it has to remove.
As an aside, you can also see that the script has run within the Execmgr.log on your test computer.
You can see from the screenshot above that after a few minutes all of the Monitor Information Reporting ARP entries were removed. Remember, in some cases, you might want your script to also reboot the computers, so if you want to do this, add to your script the command:
I hope that you find this blog post useful and if you have any questions, please feel free to contact me @GarthMJ.