Silently Uninstall Software

Table of Contents

Here is a handy script I found to uninstall software as an automated task.
This will be a lot better when GFI implement a feature to run a script once off.

I have modified it to not be case sensitive and also to match contains.
This means if you run /Program:adobe reader it will match anything that contains adobe reader in the title.

Make sure you test the uninstall codes 1st as not every program uses MSI’s to install.
Here is an example how to use it:
Silently Uninstall Software, Screwloose IT

'------------------------------------------------------------------------------ 
' magic_uninstall.vbs 
'------------------------------------------------------------------------------ 
' generic uninstaller script to uninstall applications based on the Add/Remove 
' Programs DisplayName and UninstallString properties 
'------------------------------------------------------------------------------ 
' Author: David M. Dolan 
' Created: 4/19/2005 
' Modified: 21/03/2012 (Info@Screwloose.com.au) 
'------------------------------------------------------------------------------ 
' Use this script for whatever you want -- I'm stating that there is no warranty 
' and that I'm not responsible for any damage you do with it. 
'------------------------------------------------------------------------------ 
' designed for use with MSI's but it should work on any other program that 
' specifies and uninstall string -- but you have to miff out the /Options 
' (" " works) 
'------------------------------------------------------------------------------ 


'On error resume next 

'------------------------------------------------------------------------------ 
' constants 
'------------------------------------------------------------------------------ 
Const HKEY_LOCAL_MACHINE = &H80000002 
Const KeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall" 

'modify this if you want to, I default mine for MSIs, and if you're silly enough 
' not to pass your own options and accidentally uninstall something, I want 
' the dialog to pop up telling you what you just foobarred 
Const defaultOptions = "/qb+" 
'------------------------------------------------------------------------------ 

'------------------------------------------------------------------------------ 
' read the command line paramters, and bomb out if there is a problem 
'------------------------------------------------------------------------------ 
progToRemove = WScript.Arguments.Named("Program") 

MSIOptions = WScript.Arguments.Named("Options") 
If MSIOptions = "" Then 
 MSIOptions = defaultOptions 
End If 


'------------------------------------------------------------------------------ 
' Main Program 
'------------------------------------------------------------------------------ 
If progToRemove = "" Then 
 usage 
Else 
 
 SeekAndDestroy 
 
End If 
WScript.Quit(0) 

'------------------------------------------------------------------------------ 
Sub usage 
 '------------------------------------------------------------------------------ 
 WScript.Echo "usage: magic_uninstall.vbs /Program: /Options:" 
 WScript.Echo " ex: magic_uninstall.vbs /Program:""Orca"" /Options:""/qb-""" 
 WScript.Quit(1) 
End Sub 
'------------------------------------------------------------------------------ 
Sub SeekAndDestroy 
 '------------------------------------------------------------------------------ 
 
 Set locator = CreateObject("WbemScripting.SWbemLocator") 
 Set oWMI = locator.ConnectServer(".","root/default") 
 
 
 Set objReg = oWMI.Get("StdRegProv") 
 
 lRC = objReg.EnumKey (HKEY_LOCAL_MACHINE, KeyPath, arrSubKeys) 
 
 
 'ok, so we're at the install key, loop through the sub keys... 
 For Each Subkey In arrSubKeys 
 
 'look at the next key 
 newKeyPath = KeyPath & "\" & SubKey 
 
 'get all of the values and store them in two arrays -- value names in arrEntryNames 
 '    -- value Types in to arrValueTypes 
 objReg.EnumValues HKEY_LOCAL_MACHINE,_ 
 newKeyPath,arrEntryNames,arrValueTypes 
 
 'make sure we have an array, then grok it 
 If IsArray(arrEntryNames) Then 
 
 uninstallable = "f" 
 
 'loop through the entry names and keep the ones we're looking for 
 For i = 0 To UBound(arrValueTypes) 
 entryName = arrEntryNames(i) 
 
 Select Case entryName 
 
 Case "DisplayName" 
 
 'ok, display name, so what's the value? put it into sName 
 objReg.GetStringValue HKEY_LOCAL_MACHINE, _ 
 newKeyPath, entryName, sName 
 
 Case "DisplayVersion" 
 'ok, displayVersion, so store it into sVers 
 
 objReg.GetStringValue HKEY_LOCAL_MACHINE, _ 
 newKeyPath, entryName, sVers 
 
 Case "UninstallString" 
 objReg.GetStringValue HKEY_LOCAL_MACHINE, _ 
 newKeyPath, entryName, sUninstall 
 uninstallable = "t" 
 End Select 
 
 Next '-- value 
 
 If sName  "" And uninstallable = "t" Then 
 
 'ok so we know that we have a software name, and it's uninstallable, so 
 ' only now do we check to see if it's what we're looking for... 
 
 If InStr(LCase(sName), LCase(progToRemove))  0 Then 
 
 'this is where the magic happens 
 Set oCmd = CreateObject("Wscript.Shell") 
 
 uninstallCommand = Replace(sUninstall, "/I", "/x") & " " & MSIOptions  
 commandLine = "%comspec% /c " & uninstallCommand  
 'wscript.echo commandLine 
 
 oCmd.Run commandLine, 0, True 
 wscript.Echo "UnInstalled " & sName & " Successfully!"
 WScript.Quit(0) ' haha we're done, quit digging in the registry now! 
 
 End If  
 End If 
 End If 
 
 sName = "" 
 sVers = "" 
 
 Next '-- subKey 
 wscript.Echo progToRemove & " Not Found."
End Sub

Our Services