5,421,850 members and growing! (21,696 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » General     Beginner License: The Code Project Open License (CPOL)

MSIUninstaller.exe (console application)

By thomasholme

This tool allow you to easily remove all previously installed MSI packages.
VB (VB 7.x, VB 8.0, VB 9.0, VB 6, VB), .NET (.NET, .NET 2.0), Win32, SysAdmin

Posted: 7 Aug 2008
Updated: 7 Aug 2008
Views: 1,666
Bookmarked: 4 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
1 vote for this Article.
Popularity: 0.00 Rating: 3.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 100.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
MSIUninstaller_bin

Introduction

I have a problem where I periodically have to rollback many machines on the network to an older version of our software (distributed as MSI package).
Thus I made a simple console application that can run from a bat script or similar. It allows me to uninstall MSI packages on a computer, even when I don't know exactly, which version is currently installed on the machine. And furnther if the correct (old version) is already installed then I can skip the uninstallation of the package.

Using the code

The MSIUninstaller.exe call the Microsoft MsiExec.exe to handle the actual uninstallation of the MSI package.
It looks up all the installed package in the registry database under
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
Then it loop through then to find the packages, which match the specific input parameters and then they are uninstalled.
The program allow you to specify input parameters like quiet, log and wild cards.

This function show the primary functionality of the program:

    Sub Process(ByVal DisplayName As String, ByVal bUninstall As Boolean, ByVal bDisplay As Boolean, ByVal bQuiet As Boolean, ByVal bLog As Boolean, ByVal strSkip As String)

        ' Retrieve all the subkeys for the specified key.
        Dim names As String() = m_rKey.GetSubKeyNames
        Dim version As String = ""
        Dim publisher As String = ""
        Dim dispName As String = ""
        Dim iFound As Integer

        For Each s As String In names
            dispName = GetKeyValue(s, "DisplayName")
            If dispName <> "" Then
                If WildCardCompare(dispName, DisplayName) Then
                    version = GetKeyValue(s, "DisplayVersion")
                    publisher = GetKeyValue(s, "Publisher")
                    If strSkip <> "none" And WildCardCompare(version, strSkip) Then
                        Continue For
                    End If
                    Dim cmd As String = Environment.SystemDirectory.ToString + "\MsiExec.exe /x " + s
                    If bQuiet Then
                        cmd += " /qn"
                    End If
                    If bLog Then
                        cmd += " /lie+c:\MSIUninstaller.log.txt"
                    End If
                    If Not bDisplay Then
                        Console.WriteLine("Publisher       = " + publisher)
                        Console.WriteLine("DisplayName     = " + dispName)
                        Console.WriteLine("version         = " + version)
                        Console.WriteLine("GUID            = " + s)
                        Console.WriteLine("EsitmatedSize   = " + (CDbl(GetKeyValue(s, "EstimatedSize")) / 1024).ToString("N2") + " Mb")
                        Dim dato As String = GetKeyValue(s, "InstallDate")
                        dato = dato.Substring(6) + "-" + dato.Substring(4, 2) + "-" + dato.Substring(0, 4)
                        Console.WriteLine("InstallDate     = " + CDate(dato).ToString("dd-MMM-yyy"))
                        Console.WriteLine("InstallSource   = " + GetKeyValue(s, "InstallSource") + vbLf)
                    End If
                    If bUninstall Then
                        Shell(cmd)
                    End If
                    iFound += 1
                End If
            End If
        Next s
        If Not bQuiet And iFound = 0 Then
            Console.WriteLine(DisplayName + " was not found in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall on this computer" + vbLf)
        End If
    End Sub

    Private Function GetKeyValue(ByVal keyName As String, ByVal valueName As String) As String
        Dim obj As Object = m_rKey.OpenSubKey(keyName).GetValue(valueName)
        If Not obj Is Nothing Then
            Return obj
        Else
            Return ""
        End If
    End Function

    Private Function WildCardCompare(ByVal strInput As String, ByVal strWildCard As String) As Boolean
        strWildCard = "^" + Regex.Escape(strWildCard).Replace("\*", ".*").Replace("\?", ".") + "$"
        Return Regex.IsMatch(strInput, strWildCard, RegexOptions.IgnoreCase)
    End Function

		

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

thomasholme



Occupation: Web Developer
Location: Denmark Denmark

Other popular Dialogs and Windows articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Aug 2008
Editor:
Copyright 2008 by thomasholme
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project