PoshCode Logo PowerShell Code Repository

Remote Uninstall by AlphaSun 12 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6297"></script>download | new post

This script allows you to perform application uninstalls remotely. The script prompts for an input file that contains target PCs (one on each line) and a search terms box.

Make sure you List Programs first. Don’t be to vague when specifying the Program. For instance if you type Adobe it will uninstall all Adobe products. Try Adobe Reader and List Programs to see what you get. Once you get the correct output when listing then you can uninstall. Uses MSIexec with /qn switch.

  1. ##########################################################################################
  2. ############################### Uninstaller Version 1.0 ##################################
  3. ##########################################################################################
  4. ## Built this script using multiple resources. I pulled some of the Functions and forms ##
  5. ## from the internet. I pieced them together into an all-in-one Uninstaller. Enjoy :)   ##
  6. ##########################################################################################
  7. ## Credits:                                                                             ##
  8. ## Felipe Binotto @:                                                                    ##
  9. ## http://powershell.com/cs/media/p/7673.aspx                                           ##
  10. ## Sitaram @:                                                                           ##
  11. ## http://techibee.com/powershell/powershell-uninstall-software-on-remote-computer/1400 ##
  12. ##########################################################################################
  13.  
  14.  
  15. ################################################
  16. ### Beginning of List Programs Function ########
  17. ################################################
  18.  
  19. Function ListPrograms {
  20.  
  21. [cmdletbinding()]            
  22.  
  23. [cmdletbinding()]
  24. param(
  25.  [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  26.  [string[]]$ComputerName = $env:computername            
  27.  
  28. )            
  29.  
  30. begin {
  31.  $UninstallRegKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  32. }            
  33.  
  34. process {
  35.  foreach($Computer in $ComputerName) {
  36.   Write-Verbose "Working on $Computer"
  37.   if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
  38.    $HKLM   = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computer)
  39.    $UninstallRef  = $HKLM.OpenSubKey($UninstallRegKey)
  40.    $Applications = $UninstallRef.GetSubKeyNames()            
  41.  
  42.    foreach ($App in $Applications) {
  43.     $AppRegistryKey  = $UninstallRegKey + "\\" + $App
  44.     $AppDetails   = $HKLM.OpenSubKey($AppRegistryKey)
  45.     $AppGUID   = $App
  46.     $AppDisplayName  = $($AppDetails.GetValue("DisplayName"))
  47.     $AppVersion   = $($AppDetails.GetValue("DisplayVersion"))
  48.     $AppPublisher  = $($AppDetails.GetValue("Publisher"))
  49.     $AppInstalledDate = $($AppDetails.GetValue("InstallDate"))
  50.     $AppUninstall  = $($AppDetails.GetValue("UninstallString"))
  51.     if(!$AppDisplayName) { continue }
  52.     $OutputObj = New-Object -TypeName PSobject
  53.     $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
  54.     $OutputObj | Add-Member -MemberType NoteProperty -Name AppName -Value $AppDisplayName
  55.     $OutputObj | Add-Member -MemberType NoteProperty -Name AppVersion -Value $AppVersion
  56.     $OutputObj | Add-Member -MemberType NoteProperty -Name AppVendor -Value $AppPublisher
  57.     $OutputObj | Add-Member -MemberType NoteProperty -Name InstalledDate -Value $AppInstalledDate
  58.     $OutputObj | Add-Member -MemberType NoteProperty -Name UninstallKey -Value $AppUninstall
  59.     $OutputObj | Add-Member -MemberType NoteProperty -Name AppGUID -Value $AppGUID
  60.     $OutputObj# | Select ComputerName, DriveName
  61.    }
  62.   }
  63.  }
  64. }            
  65.  
  66. end {}
  67. }
  68.  
  69. ################################################
  70. ### End of the List Programs Function ##########
  71. ################################################
  72.  
  73. ################################################
  74. ### Beginning of Uninstall Programs Function ###
  75. ################################################
  76.  
  77. Function UninstallProgram {
  78.  
  79. [cmdletbinding()]            
  80.  
  81. param (            
  82.  
  83.  [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  84.  [string]$ComputerName = $env:computername,
  85.  [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
  86.  [string]$AppGUID
  87. )            
  88.  
  89.  try {
  90.   $returnval = ([WMICLASS]"\\$computerName\ROOT\CIMV2:win32_process").Create("msiexec `/x$AppGUID `/qn")
  91.  } catch {
  92.   write-error "Failed to trigger the uninstallation. Review the error message"
  93.   $_
  94.   exit
  95.  }
  96.  switch ($($returnval.returnvalue)){
  97.   0 { "Uninstallation command triggered successfully" }
  98.   2 { "You don't have sufficient permissions to trigger the command on $Computer" }
  99.   3 { "You don't have sufficient permissions to trigger the command on $Computer" }
  100.   8 { "An unknown error has occurred" }
  101.   9 { "Path Not Found" }
  102.   9 { "Invalid Parameter"}
  103.  }
  104.  
  105. }
  106. ################################################
  107. ### End of Uninstall Programs Function #########
  108. ################################################
  109.  
  110. ################################################
  111. ### Start of ForEach Functions #################
  112. ################################################
  113.  
  114. Function FElist {
  115. $ServerList = Get-Content $Address.text
  116. $ProgramName = $program.Text
  117. $OutputListArray= @()
  118. foreach($Server in $ServerList){
  119. $ListResults= ListPrograms -ComputerName $Server | Where-Object {$_.AppName -like "*$ProgramName*"} | Select-Object `
  120. ComputerName, AppName, AppVersion | Format-Table -AutoSize
  121. $OutputListArray += $ListResults
  122. }
  123. $OutputListArray | out-file $env:userprofile\documents\temp_programs.txt
  124. $ListFile= "$env:userprofile\documents\temp_programs.txt"
  125. Invoke-Item $ListFile
  126. }
  127.  
  128. Function FEuninstall {
  129. $ServerList = Get-Content $Address.text
  130. $ProgramName = $program.Text
  131. $OutputProgArray= @()
  132. foreach($Server in $ServerList){
  133. $UninstResult= ListPrograms -ComputerName $Server | Where-Object {$_.AppName -like "*$ProgramName*"} | `
  134. UninstallProgram
  135. $OutputProgArray += "$Server $ProgramName " + $UninstResult
  136. }
  137. $OutputProgArray | out-file $env:userprofile\documents\temp_uninst.txt
  138. $Progfile= "$env:userprofile\documents\temp_uninst.txt"
  139. Invoke-Item $Progfile
  140. }
  141. ################################################
  142. ### End of ForEach Functions $##################
  143. ################################################
  144.  
  145. ##############################################################
  146. ### Beginning of Windows Forms Worker ########################
  147. ##############################################################
  148.  
  149.     [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  150.     [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  151.      
  152.     $Form = New-Object System.Windows.Forms.Form
  153.      
  154.     $Form.width = 500
  155.     $Form.height = 300
  156.     $Form.Text = "Remote Uninstall"
  157.     $Form.backcolor = "#5D8AA8"
  158.     $Form.maximumsize = New-Object System.Drawing.Size(500, 300)
  159.     $Form.startposition = "centerscreen"
  160.     $Form.KeyPreview = $True
  161.     $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")  
  162.         {$Form.Close()}})
  163.  
  164.     $Infolabel = new-object System.Windows.Forms.Label
  165.     $Infolabel.Location = new-object System.Drawing.Size(20,10)
  166.     $Infolabel.size = new-object System.Drawing.Size(450,80)
  167.     $Infolabel.Font = new-object System.Drawing.Font("Microsoft Sans Serif",10,[System.Drawing.FontStyle]::Bold)
  168.     $Infolabel.Text = "!! INFORMATION : Make sure you List Programs first. Don't be to vague when specifying the" +
  169.     " Program. For instance if you type Adobe it will unintall all adobe products. Try Adobe Reader and List" +
  170.     " Programs to see what you get. Once you get the correct output when listing then you can uninstall. Uses" +
  171.     " MSIexec with /qn switch."
  172.     $Form.Controls.Add($Infolabel)
  173.      
  174.     $ListButton = new-object System.Windows.Forms.Button
  175.     $ListButton.Location = new-object System.Drawing.Size(50, 200)
  176.     $ListButton.Size = new-object System.Drawing.Size(130,30)
  177.     $ListButton.Text = "List Programs"
  178.     $ListButton.FlatAppearance.MouseOverBackColor = [System.Drawing.Color]::FromArgb(255, 255, 192);
  179.     $ListButton.ImageAlign = [System.Drawing.ContentAlignment]::MiddleLeft;
  180.     $Listbutton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
  181.     $ListButton.Add_Click({FElist})
  182.      
  183.     $Form.Controls.Add($ListButton)
  184.      
  185.     $address = new-object System.Windows.Forms.TextBox
  186.     $address.Location = new-object System.Drawing.Size(45,160)
  187.     $address.Size = new-object System.Drawing.Size(220,20)
  188.      
  189.     $Form.Controls.Add($address)
  190.      
  191.     $addresslabel = new-object System.Windows.Forms.Label
  192.     $addresslabel.Location = new-object System.Drawing.Size(45,110)
  193.     $addresslabel.size = new-object System.Drawing.Size(170,50)
  194.     $addresslabel.Font = new-object System.Drawing.Font("Microsoft Sans Serif",12,[System.Drawing.FontStyle]::Bold)
  195.     $addresslabel.Text = "Path to list of Computers:"
  196.     $Form.Controls.Add($addresslabel)
  197.      
  198.     $programlabel = new-object System.Windows.Forms.Label
  199.     $programlabel.Location = new-object System.Drawing.Size(285,110)
  200.     $programlabel.size = new-object System.Drawing.Size(140,50)
  201.     $programlabel.Font = new-object System.Drawing.Font("Microsoft Sans Serif",12,[System.Drawing.FontStyle]::Bold)
  202.     $programlabel.Text = "Program to Uninstall:"
  203.     $Form.Controls.Add($programlabel)
  204.      
  205.     $program = new-object System.Windows.Forms.TextBox
  206.     $program.Location = new-object System.Drawing.Size(285,160)
  207.     $program.Size = new-object System.Drawing.Size(160,20)
  208.    
  209.     $Form.Controls.Add($program)
  210.  
  211.     $UninstallButton = new-object System.Windows.Forms.Button
  212.     $UninstallButton.Location = new-object System.Drawing.Size(300,200)
  213.     $UninstallButton.Size = new-object System.Drawing.Size(130,30)
  214.     $UninstallButton.FlatAppearance.MouseOverBackColor = [System.Drawing.Color]::FromArgb(255, 255, 192);
  215.     $UninstallButton.ImageAlign = [System.Drawing.ContentAlignment]::MiddleLeft;
  216.     $UninstallButton.Text = "Uninstall"
  217.     $Uninstallbutton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
  218.     $UninstallButton.Add_Click({FEuninstall})
  219.  
  220.      
  221.     $viclabel = new-object System.Windows.Forms.Label
  222.     $viclabel.Location = new-object System.Drawing.Size(140,250)
  223.     $viclabel.size = new-object System.Drawing.Size(200,50)
  224.     $Form.Controls.Add($viclabel)
  225.      
  226.     $Form.Controls.Add($UninstallButton)
  227.      
  228.     $Form.Add_Shown({$Form.Activate()})
  229.     $Form.ShowDialog()
  230.  
  231.  
  232. ##############################################################
  233. ### End of Windows Forms Worker ##############################
  234. ##############################################################

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me