18 February 2016

PowerShell MSI Uninstaller By Application Name

This is now an old version. The new version is now located in this blog posting

Here is a function that will uninstall an MSI installed application by the name of the app. You do not need to input the entire name either. For instance, say you are uninstalling all previous versions of Adobe Reader. Adobe Reader is always labeled Adobe Reader X, Adobe Reader XI, and so forth. This script allows you to do this without having to find out every version that is installed throughout a network and then enter an uninstaller line for each version. You just need to enter Adobe Reader as the application name and the desired switches. It will then search the name fields in the 32 and 64 bit uninstall registry keys to find the associated GUID. Finally, it will execute an msiexec.exe /x {GUID} to uninstall that version.

NOTE: I used Sapien's PowerShell Studio to write this script that significantly simplified the process and made it a snap to write. I highly recommend this product for all PowerShell scripters!

You can download the script from here.

 <#       
      .NOTES  
      ===========================================================================  
       Created with:      SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.99  
       Created on:       2/18/2016 12:32 PM  
       Created by:       Mick Pletcher  
       Organization:         
       Filename:          
      ===========================================================================  
      .DESCRIPTION  
           Here is a function that will uninstall an MSI installed application by  
           the name of the app. You do not need to input the entire name either.   
           For instance, say you are uninstalling all previous versions of Adobe   
           Reader. Adobe Reader is always labeled Adobe Reader X, Adobe Reader XI,  
           and so forth. You just need to enter Adobe Reader as the application   
           name and the desired switches. It will then search the name fields in   
           the 32 and 64 bit uninstall registry keys to find the associated GUID.   
           Finally, it will execute an msiexec.exe /x {GUID} to uninstall that   
           version.   
 #>  
   
 Function Uninstall-MSIByName {  
    <#   
    .SYNOPSIS   
       Uninstall-MSIByName   
    .DESCRIPTION   
       Uninstalls an MSI application using the MSI file   
    .EXAMPLE   
       Uninstall-MSIByName -ApplicationName "Adobe Reader" -Switches "/qb- /norestart"   
    #>       
        
      Param ([String]$ApplicationName,  
           [String]$Switches)  
        
      #Declare Local Variables   
      Set-Variable -Name ErrCode -Scope Local -Force  
      Set-Variable -Name Executable -Scope Local -Force  
      Set-Variable -Name Key -Scope Local -Force  
      Set-Variable -Name KeyName -Scope Local -Force  
      Set-Variable -Name Parameters -Scope Local -Force  
      Set-Variable -Name SearchName -Scope Local -Force  
      Set-Variable -Name TempKey -Scope Local -Force  
      Set-Variable -Name Uninstall -Scope Local -Force  
        
      $Uninstall = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Recurse -ea SilentlyContinue  
      $Uninstall += Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall -Recurse -ea SilentlyContinue  
      $SearchName = "*" + $ApplicationName + "*"  
      $Executable = $Env:windir + "\system32\msiexec.exe"  
      Foreach ($Key in $Uninstall) {  
           $TempKey = $Key.Name -split "\\"  
           If ($TempKey[002] -eq "Microsoft") {  
                $Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + $Key.PSChildName  
           } else {  
                $Key = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" + $Key.PSChildName  
           }  
           If ((Test-Path $Key) -eq $true) {  
                $KeyName = Get-ItemProperty -Path $Key  
                If ($KeyName.DisplayName -like $SearchName) {  
                     $TempKey = $KeyName.UninstallString -split " "  
                     If ($TempKey[0] -eq "MsiExec.exe") {  
                          Write-Host "Uninstall"$KeyName.DisplayName"....." -NoNewline  
                          $Parameters = "/x " + $KeyName.PSChildName + [char]32 + $Switches  
                          $ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Parameters -Wait -Passthru).ExitCode  
                          If (($ErrCode -eq 0) -or ($ErrCode -eq 3010) -or ($ErrCode -eq 1605)) {  
                               Write-Host "Success" -ForegroundColor Yellow  
                          } else {  
                               Write-Host "Failed with error code "$ErrCode -ForegroundColor Red  
                          }  
                     }  
                }  
           }  
      }  
        
      #Cleanup Local Variables   
      Remove-Variable -Name ErrCode -Scope Local -Force  
      Remove-Variable -Name Executable -Scope Local -Force  
      Remove-Variable -Name Key -Scope Local -Force  
      Remove-Variable -Name KeyName -Scope Local -Force  
      Remove-Variable -Name Parameters -Scope Local -Force  
      Remove-Variable -Name SearchName -Scope Local -Force  
      Remove-Variable -Name TempKey -Scope Local -Force  
      Remove-Variable -Name Uninstall -Scope Local -Force  
        
 }  
   
 Uninstall-MSIByName -ApplicationName "Adobe Reader" -Switches "/qb- /norestart"  

Related Posts:

  • Add/Remove Program entries Sometimes it is necessary to add an add/remove programs entry. There are instances where an application is independent and requires not installation and you want to make sure it is copied to the system, or you use add/remov… Read More
  • Disable Windows Media Center If you are needing to disable Windows Media Center in Windows 7 by command line, here is a script that will do just that. This script will not only disable it, but it will also add an add/remove programs and HKCR entry so t… Read More
  • Dell Client System Update for the SCCM & MDT Build The DCSU is a great utility that Dell has made available to update the drivers and driver applications on Dell systems. The industry I work in requires specific drivers for specific applications, which makes the DCSU not a… Read More
  • Verify applications were installed during build process As many of you have probably experienced, SCCM and MDT do not always install all of the applications that are in the task sequence list, even if it returned an error code 0. This can be rather annoying, especially when it h… Read More
  • Autodesk 2014 Building Design Suite Ultimate Uninstaller The Autodesk suite is not the easiest to uninstall because of all the components that the built in uninstaller does not uninstall. I went in and extracted all of the GUIDs for each components and created the powershell scr… Read More

0 comments:

Post a Comment