Finding and uninstalling applications using VBScript

Have you ever wanted to automate the installation of several programs but had issues with older versions still being there?  Below is a cool little script that uses Adobe Reader as an example. Check out this MSDN article about the StdRegProv class provided with WMI for more detail.  Read through it and then we’ll walk through the sections below!  You will need to have a basic understanding of VBScript and WMI.  Check my cool links on the side bar for tutorials.
 
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oShell = WScript.CreateObject("WScript.Shell")
Set StdOut = WScript.StdOut
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
err.Clear
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
If err.Number <> 0 Then
  StdOut.WriteLine strKeyPath & " is not a valid registry key path."
  WScript.Quit
End If
For Each subkey In arrSubKeys
  strSubKeyPath = strKeyPath & "\" & subkey
  oReg.EnumValues HKEY_LOCAL_MACHINE, strSubKeyPath, arrValueNames, arrValueTypes
  If IsArray(arrValueNames) Then
    For Each strValue In arrValueNames
      If strValue = "DisplayName" Then
        err.Clear
        GetValueStatus = oReg.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strSubKeyPath, strValue, strDisplayName)
        If (GetValueStatus <> 0) and (err.Number <> 0) Then
          StdOut.WriteLine "Key Name: " & subkey
          StdOut.WriteLine "    Error retrieving value data."
        Else
          If (InStr(1, strDisplayName, "Adobe", 1) <> 0) AND (InStr(1, strDisplayName, "Reader", 1) <> 0) Then
            StdOut.WriteLine "Key Name: " & subkey
            StdOut.WriteLine "  Application Name: " & strDisplayName
            GetValueStatus = oReg.GetExpandedStringValue _
              (HKEY_LOCAL_MACHINE, strSubKeyPath, "DisplayVersion" , strDisplayVersion)
            arrAppVersion = Split(strDisplayVersion, ".")
            If arrAppVersion(0) < 8 Then
              GetValueStatus = oReg.GetExpandedStringValue _
                (HKEY_LOCAL_MACHINE, strSubKeyPath, "UninstallString" , strUninstallString)
              If InStr(1, strUninstallString, "msiexec", 1) <> 0 Then
                If InStr(1, strUninstallString, "/I", 1) <> 0 Then
                  strUninstallString = Replace(strUninstallString, "/I", "/X")
                End If 
                If InStr(1, strUninstallString, "/q", 1) = 0 Then
                  strUninstallString = strUninstallString & " /qb"
                End If
                If InStr(1, strUninstallString, "restart", 1) = 0 Then
                  strUninstallString = strUninstallString & " /norestart"
                End If
              End If 
              StdOut.WriteLine "    Uninstall String: " & strUninstallString
              oShell.Run strUninstallString, 1, True
            Else
              StdOut.WriteLine "    Current Version Installed."
            End If
          End If
        End If
      End If
    Next
  End If
Next
 
So here’s a basic explanation of our code.
 
Here we instantiate the StdRegProv class and assign it to the variable oReg, which we will use later to call methods of the class.
  Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
This is the registry key that contains a list of all the installed application on your computer.  This is also a great place to do an inventory!
  Windows XP x86
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
  Windows XP x64 (you’ll need to edit the script for this)
    strKeyPath = "Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
 
Now will enumerate the Uninstall registry key and place the key names in the array arrSubKeys.  Each installed application has it’s own subkey of Uninstall.
  oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
Next we start a loop to deal with each subkey under the Uninstall registry key.  The variable subkey will be assigned the name of the registry key under Uninstall.
  For Each subkey In arrSubKeys
 
We now create a variable that includes our original registry path and appends the current subkey we are working with. We then call the EnumValua method supply the full path to the subkey and the value names will be stored in the array arrValueNames.
  strSubKeyPath = strKeyPath & "\" & subkey
  oReg.EnumValues HKEY_LOCAL_MACHINE, strSubKeyPath, arrValueNames, arrValueTypes
 
Then we check to see if arrValueNames is truly and array.  Some keys may contain no values and therefore will not be an array.  By using the IF statement we can bypass any empty keys.   For each subkey that was able to return an aeeay of value we then cycle through each value looking for the labeled "DisplayName"
  If IsArray(arrValueNames) Then
  For Each strValue In arrValueNames
  If strValue = "DisplayName" Then
 
Now we are able to retrieve the data from the registry key value.  We provide the subkey path and the value name and the data is returned in the strDisplayName variable.
  GetValueStatus = oReg.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strSubKeyPath, strValue, strDisplayName)
 
Next we check the strDisplayName variable to see if the key is for the program we are looking for.  This can be changed to suite your need of course!
  If (InStr(1, strDisplayName, "Adobe", 1) <> 0) AND (InStr(1, strDisplayName, "Reader", 1) <> 0) Then

If it is we then pull the data for the "DisplayVersion" value under the subkey we are working with.  Then we create a new array by using the Split function to seperate the version number by periods.  This can be handled in any number of ways to meet varying version numbering standards.  In the case of Adobe Reader we are looking for versions before 8.
  GetValueStatus = oReg.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strSubKeyPath, "DisplayVersion" , strDisplayVersion)
  arrAppVersion = Split(strDisplayVersion, ".")
  If arrAppVersion(0) < 8 Then
 
Once we have determined that we have an version we want to remove we [ull the data for the value "UninstallString" and check it to see if it meets are needs.  Since there are several installer type you will have to do some research.  For this example we are dealing with a Windows Installer application.  We check this by searching for the string "msiexec".  Then we replace the /I option with /X to perform and uninstall.  In most cases ther is no /q option in the uninstall string so we add a /qb which shows us a progress bar only.  Then we add the /norestart option so we don’t reveive any prompts or unexpected reboots.
  GetValueStatus = oReg.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strSubKeyPath, "UninstallString" , strUninstallString)
  If InStr(1, strUninstallString, "msiexec", 1) <> 0 Then
    If InStr(1, strUninstallString, "/I", 1) <> 0 Then
      strUninstallString = Replace(strUninstallString, "/I", "/X")
    End If 
    If InStr(1, strUninstallString, "/q", 1) = 0 Then
      strUninstallString = strUninstallString & " /qb"
    End If
    If InStr(1, strUninstallString, "restart", 1) = 0 Then
      strUninstallString = strUninstallString & " /norestart"
    End If
  End If  
 
Now the actual work happens!  strUninstallString is the custom uninstall string we created (which looks something like "msiexec /X {BigLongCSLID} /qb /norestart").  We execute it with the options 1 and True.  1 is a constant for using default window behavior and True makes the script wait until the executable we executed finishes running.
  oShell.Run strUninstallString, 1, True
 
So there you have it!  How to automate the detection and removal of an application using VBScript!  Please comment if you have any questions!
Advertisements

One response to “Finding and uninstalling applications using VBScript

  1. Hi,
    i’m new in scripting and i would ask you. What do i edit when i would uninstal Microsoft Operating Manager 2005 agent? His display name is Microsoft Operating Manager 2005 and uninstall string is c:\windows\system32\msiexec.exe.
    Thank you very much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s