Simple VBS for uninstaling software.

As per Wikipedia, VBScript (Visual Basic Scripting Edition) is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It is designed as a “lightweight” language with a fast interpreter for use in a wide variety of Microsoft environments.

Well, some use it to create GUI to track the killers IP address … but I prefer to use it in more proper way :)
Visual Basic scripting comes in hand in a large variety of situations, in other words, every sysadmin should know at least basics of VBScript.  Bellow is an example of cool VBS to uninstal software using its name displayed in Add/Remove Programs.

Question: Why would you need such script for? The answer is simple, to batch uninstall software remotely.

Example from my work experience. Uninstall Citrix MetaFrame Presentation Server Client from 150 workstations. To maximally automate this task, I’ve created this script then launched it using PSTools and Task Scheduler to run the script within our maintenance window, which starts at midnight.  The whole process kicked in by itself, without any user (or even myself) interaction and finished after one hour. Simples!

So here’s the script:

for .exe installers:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colSoftware = objWMIService.ExecQuery _
    ("Select * from Win32_Product Where Name = 'Software name from Add/Remove'")

For Each objSoftware in colSoftware
    objSoftware.Uninstall()
Next

for .msi installers.

Const ForAppending = 8

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\report.txt", ForAppending, True)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colSoftware = objWMIService.ExecQuery _
("Select * from Win32_Product Where Name = 'Software name from Add/Remove'")

For Each objSoftware in colSoftware
objSoftware.Uninstall()
objTextFile.WriteLine(WshNetwork.ComputerName)
Next

 

Leave a Reply

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