?
Solved

Uninstall by Registry GUID

Posted on 2009-02-24
6
Medium Priority
?
2,752 Views
Last Modified: 2012-05-06
I need a script to uninstall a product based on it's GUID.  The problem I'm facing is that there are various updates and versions of the product in our environment, and I would rather not write 100 If/Else statements to handle all the different GUID's .  I'd like to search the registry for a particular Display Name (that is always the same despite version) and then use the information found in the Uninstall String to uninstall the product.
Below is the code I have so far, but it isn't working.  Any help would be greatly appreciated!!
Const HKEY_LOCAL_MACHINE = &H80000002
 
strComputer = "."
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
    subKeyPath = strKeyPath & "\" & subkey
    strValueName = "DisplayName"
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,subKeyPath,strValueName,dwValue
 
    If dwValue = "Adobe Acrobat" Then
      strValueName = "UninstallString"
      objRegistry.GetStringValue HKEY_LOCAL_MACHINE,subKeyPath,strValueName,dwValue
      dwValue = Replace(dwValue, "/I", "/x")
      dwValue = dwValue + " /q"
 
      Set objShell = WScript.CreateObject("WScript.Shell")
      objShell.Run(dwValue),1,True
    Else
    End If
 
If dwValue = "Adobe Reader" Then
      strValueName = "UninstallString"
      objRegistry.GetStringValue HKEY_LOCAL_MACHINE,subKeyPath,strValueName,dwValue
      dwValue = Replace(dwValue, "/I", "/x")
      dwValue = dwValue + " /q"
 
      Set objShell = WScript.CreateObject("WScript.Shell")
      objShell.Run(dwValue),1,True
    Else
    End If
Next

Open in new window

0
Comment
Question by:BalchBingham
  • 3
  • 3
6 Comments
 
LVL 14

Accepted Solution

by:
yehudaha earned 2000 total points
ID: 23725676
i think thsi will do it

test it:
strComputer = "."
strSoftName = InputBox("insert software name")
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Product Where Name = "& strSoftName)
For Each objItem in colItems
	WScript.Echo UninstallByGuid(objItem.IdentifyingNumber) 
Next
 
Function UninstallByGUID(sGUID)   
         Set oWMIColl = oWMIroot.ExecQuery("Select * FROM Win32_Product Where IdentifyingNumber = '" & sGUID & "'") 
         For Each oWMIval In oWMIColl
         oWMIval.Uninstall() 
         UninstallByGUID = "Removal of " & oWMIval.Name & " complete (" & err.number & ")"
         Next 
         If UninstallByGUID = "" Then UninstallByGUID = "Application GUID not found."        
 End Function

Open in new window

0
 

Author Comment

by:BalchBingham
ID: 23726398
The above code errors out.  I don't believe it's executing the query correctly.  I get a Windows Script Host Error, Line 6 Char 1, Source = (null).  I'm working on it, but I wanted to respond to see if you can find the error faster.
Thanks.
0
 
LVL 14

Assisted Solution

by:yehudaha
yehudaha earned 2000 total points
ID: 23726587
maybe you don't enter you software name propely

so run this it will echo all software installed then you can see the right way you need to enter the software name
strComputer = "."
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Product)
For Each objItem in colItems
        WScript.Echo objItem.caption
Next

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BalchBingham
ID: 23727093
I've changed the Software Name it is looking for to a "Like", but now I am getting an Object Required: 'oWMIroot" Error.
Line 11, Char 10.

strComputer = "."
strSoftName = "Adobe Acrobat"
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Product Where Name like '" & strSoftName & "%'")
For Each objItem in colItems
	WScript.Echo UninstallByGuid(objItem.IdentifyingNumber) 
Next
 
Function UninstallByGUID(sGUID)   
         Set oWMIColl = oWMIroot.ExecQuery("Select * FROM Win32_Product Where IdentifyingNumber = '" & sGUID & "'") 
         For Each oWMIval In oWMIColl
         oWMIval.Uninstall() 
         UninstallByGUID = "Removal of " & oWMIval.Name & " complete (" & err.number & ")"
         Next 
         If UninstallByGUID = "" Then UninstallByGUID = "Application GUID not found."        
 End Function

Open in new window

0
 

Author Comment

by:BalchBingham
ID: 23727200
I added the line:
Set oWMIroot = SWBemlocator.ConnectServer(strComputer,"root\CIMV2")
and it works!!  Thanks for the help!  Final code is posted below for anyone that may need to uninstall a product with multiple GUID's....  Just change the "strSoftName =" to whatever application is needed.  
Thanks again!

 

strComputer = "."
strSoftName = "Adobe Acrobat"
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Product Where Name like '" & strSoftName & "%'")
For Each objItem in colItems
	WScript.Echo UninstallByGuid(objItem.IdentifyingNumber) 
Next
 
Function UninstallByGUID(sGUID)   
Set oWMIroot = SWBemlocator.ConnectServer(strComputer,"root\CIMV2") 
        Set oWMIColl = oWMIroot.ExecQuery("Select * FROM Win32_Product Where IdentifyingNumber = '" & sGUID & "'") 
         For Each oWMIval In oWMIColl
         oWMIval.Uninstall() 
         UninstallByGUID = "Removal of " & oWMIval.Name & " complete (" & err.number & ")"
         Next 
         If UninstallByGUID = "" Then UninstallByGUID = "Application GUID not found."        
 End Function
 
msgbox ("All done")

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23730351
Glad to Help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question