Advertisement
maximillianx

Remove-MSIApps.ps1

Jan 2nd, 2014
1,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires –Version 3
  2. <#
  3. .SYNOPSIS
  4.  
  5.     Remove-MSIApps is a script designed to batch remove MSI-based applications from a remote or local computer.  
  6.  
  7. .DESCRIPTION
  8.  
  9.     This script will generate a list of installed applications in GridView.  You can then select and highlight
  10.     applications you wish to remove.  Once you click the 'OK' button, Remove-MSIApps will iterate  through each
  11.     application using the Win32_product WMI class and execute the uninstall method against them.  Note that
  12.     you can run this against a remote computer by using the -ComputerName parameter.
  13.  
  14. .PARAMETER ComputerName <String[]>
  15.     Specifies the target computer for the management operation. Enter a fully qualified domain name, a NetBIOS name, or an IP address. When the remote computer is in a different domain than the local computer, the fully qualified domain name is required.
  16.  
  17. .LINK
  18.     http://community.spiceworks.com
  19.  
  20. .NOTE
  21.     PowerShell 3.0 and above is required for script operation.
  22.  
  23.     Remote registry access is required and the account running the script should have administrative privileges on the remote system.
  24. #>
  25.  
  26. [CmdletBinding()]
  27. Param(
  28.     [Parameter(ValueFromPipeline=$true,Position=0)] [array] $ComputerName = $env:COMPUTERNAME
  29. )
  30.  
  31. BEGIN {
  32.  
  33. }
  34. PROCESS {
  35.  
  36.     Clear-Host
  37.     $keys = @()
  38.     $AppList = @()
  39.     $OverallApplicationCount = 0
  40.  
  41.     If ((Get-WmiObject win32_processor -ComputerName $ComputerName).AddressWidth -eq 32)
  42.     {   Write-Verbose "32-bit system detected."
  43.         $UninstallKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  44.         $keys += "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  45.     }
  46.     Else
  47.     {   Write-OUtput "64-bit system detected, iterating through 32-bit and 64-bit reg keys..."
  48.         $UninstallKey = "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  49.         $keys += "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  50.         $keys += "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  51.     }
  52.     Do {
  53.         $Reg = ([microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine', $ComputerName))
  54.         $AppList = $null
  55.         ForEach ($regitem in $keys) {
  56.             $RegKey = $Reg.OpenSubKey($regitem)
  57.             $SubKeys = $RegKey.GetSubKeyNames()
  58.        
  59.             $Count = 0
  60.             $Data = ForEach ($Key in $SubKeys)
  61.             {   $thisKey = $regitem + "\\" + $Key
  62.                 $thisSubKey = $Reg.OpenSubKey($thisKey)
  63.  
  64.                 $DisplayName = $thisSubKey.GetValue("DisplayName")
  65.                 If ($thisSubKey.GetValue("UninstallString") -like "*msiexec*")
  66.                 {   New-Object PSObject -Property @{
  67.                         UninstallString = $thisSubKey.GetValue("UninstallString")
  68.                         DisplayName = $DisplayName
  69.                         Publisher = $thisSubKey.GetValue("Publisher")
  70.                         DisplayVersion = $thisSubKey.GetValue("DisplayVersion")
  71.                         InstallLocation = $thisSubKey.GetValue("InstallLocation")
  72.                         GUID = $($thisSubKey.GetValue("UninstallString")).Split("{}")[1]
  73.                     }
  74.                 }
  75.                 Write-Progress -Activity "Found $($SubKeys.Count) apps on $ComputerName, filtering MSI" -Status "Found application $DisplayName" -PercentComplete ($Count / $SubKeys.Count*100)
  76.                 $Count ++
  77.             }
  78.             $AppList += $data
  79.             $OverallApplicationCount += $count
  80.         }
  81.  
  82.         Write-Output "$OverallApplicationCount Applications found. `n"
  83.         #Write-Output ""
  84.         Write-Output "Use [Ctrl]+click to highlight the applications you wish to uninstall and press [OK].  Note that some apps might be dependent upon other apps, so you may need to only uninstall the 'parent' application to remove the children."
  85.         Write-Progress -Activity "Done" -Status "Done" -Completed
  86.         $Return = $AppList | Select DisplayName,Publisher,DisplayVersion,InstallLocation,UninstallString,GUID | Sort DisplayName | Out-GridView -Title "MSI Applications on $computername" -PassThru
  87.    
  88.         If ($Return)
  89.         {  
  90.             Clear-Host
  91.  
  92.             Write-Output "Applications selected: `n"
  93.             Write-Output $Return | select Displayname, GUID
  94.  
  95.             #prompt to let user abort if file sources aren't correct
  96.             #-------------------------------------------------------------
  97.             $title      = "Uninstall the following apps from $($computername)?"
  98.             $message    = "You've selected the above apps for removal, do you wish to continue?"
  99.             $yes        = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Removes application(s) from $computername."
  100.             $no         = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Returns to application listing."
  101.             $options    = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  102.             $result     = $host.ui.PromptForChoice($title, $message, $options, 0)
  103.  
  104.             write-output  "`n"
  105.             switch ($result)
  106.                 {
  107.                     0 {
  108.                         write-output "Performing (batch) uninstallation via MSI."
  109.                     }
  110.                     1 {
  111.                         write-output "Refreshing app listing."
  112.                     }
  113.                 }
  114.  
  115.         If ($result -eq 0)
  116.         {
  117.             ForEach ($App in $Return)
  118.                 {  
  119.                     Write-Output "Uninstalling $($App.DisplayName) from $ComputerName using GUID: $($App.GUID)"
  120.                     $Return = (Get-WmiObject -Class Win32_Product -Filter "IdentifyingNumber='{$($App.GUID)}'" -ComputerName $ComputerName).Uninstall()
  121.                     if ($Return.ReturnValue -eq 0)
  122.                     {   Write-Output "`n Uninstallation of $($app.displayname) successful!"
  123.                     }
  124.                     Else
  125.                     {   Write-Output "`n Uninstallation of $($app.displayname) failed!  Error code: $($Return.ReturnValue)"
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     } Until ($Return -eq $null)
  131. }
  132.  
  133. END {
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement