Blog

Silently Uninstall Software

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'------------------------------------------------------------------------------ 
' 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:<DisplayNameString> /Options:<commandlineopts>" 
 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

Posted in: Scripts, Technical

Leave a Comment (0) ↓

Leave a Comment