Advertisement
rubber_duck13

Uninstall Remotely v2

Jul 11th, 2018
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. param(
  3.  
  4.     [Parameter(Mandatory=$true)]
  5.     [String]$Computername
  6. )
  7.  
  8. $UninstallKeys = @(
  9.  
  10.     "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
  11.     "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  12. )
  13.  
  14. $appNumber = 0
  15.  
  16. function InstalledList {
  17.  
  18.     foreach($UninstallKey in $UninstallKeys) {
  19.  
  20.         #Create an instance of the Registry Object and open the HKLM base key
  21.         $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computername)
  22.  
  23.         #Drill down into the Uninstall key using the OpenSubKey Method
  24.         $Regkey = $Reg.OpenSubKey($UninstallKey)
  25.  
  26.         #Retrieve an array of string that contain all the subkey names
  27.         $Subkeys = $Regkey.GetSubKeyNames()
  28.  
  29.         foreach($key in $subkeys) {
  30.  
  31.             $thisKey=$UninstallKey+"\\"+$key
  32.             $thisSubKey=$reg.OpenSubKey($thisKey)
  33.  
  34.             [PSCustomObject] @{
  35.  
  36.                 #AppNumber = $appNumber++
  37.                 ComputerName = $Computername
  38.                 DisplayName = $thisSubKey.GetValue("DisplayName")
  39.                 DisplayVersion = $thisSubKey.GetValue("DisplayVersion")
  40.                 InstallLocation = $thisSubKey.GetValue("InstallLocation")
  41.                 Publisher = $thisSubKey.GetValue("Publisher")
  42.                 UninstallString = $thisSubKey.GetValue("UninstallString")
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. $Apps = InstalledList | Where-Object { $_.DisplayName -and $_.UninstallString -like "MsiExec*" } | Sort-Object DisplayName | Select-Object @{Name="AppNumber";Expression={$global:appnumber; $global:appnumber++};}, DisplayName, Publisher, DisplayVersion, ComputerName, UninstallString
  50. $Apps | Format-Table -AutoSize
  51.  
  52. $uninstallApp = Read-Host -Prompt "Select an App to uninstall" -Verbose
  53. $uninstallProg = $apps | Where-Object {$_.appNumber -eq $uninstallApp}
  54. $UninstallStr =  ($uninstallProg).uninstallString -replace "/I", "/X"
  55.  
  56. Write-Verbose -Message "Uninstalling $(($uninstallProg).DisplayName) on $(($uninstallProg).ComputerName)..." -Verbose
  57.  
  58. Invoke-Command -ComputerName $Computername -ScriptBlock {
  59.  
  60.     $UninstallStr = $args[0]
  61.     $softwareName = $args[1]
  62.     $logFile = "C:\temp\$($softwarename -replace '\s', '')_uninstall.log"
  63.     $arguments = (($UninstallStr -replace "MsiExec.exe ", "") -replace "msiexec", "") + " /QN /L*V $logFile REBOOT=R"
  64.  
  65.     #Execute the software installer
  66.     Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait -Passthru
  67. } -ArgumentList $UninstallStr, ($uninstallProg).DisplayName
  68.  
  69. Pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement