Advertisement
rubber_duck13

Uninstall Remotely

Mar 15th, 2018
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param (
  3.     [Parameter(Mandatory)][string]$computername
  4. )
  5.  
  6. $InstalledList = @()
  7.  
  8.     #Define the variable to hold the location of Currently Installed Programs
  9.     $UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  10.     $UninstallKey64="SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
  11.  
  12.     #Create an instance of the Registry Object and open the HKLM base key
  13.     $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
  14.  
  15.     #Drill down into the Uninstall key using the OpenSubKey Method
  16.     $regkey=$reg.OpenSubKey($UninstallKey)
  17.     $regkey64=$reg.OpenSubKey($UninstallKey64)
  18.  
  19.     #Retrieve an array of string that contain all the subkey names
  20.     $subkeys=$regkey.GetSubKeyNames()
  21.     $subkeys64=$regkey64.GetSubKeyNames()
  22.  
  23.     # Add a number selector to each line.
  24.     $appNumber = 1
  25.  
  26.     #Open each Subkey and use GetValue Method to return the required values for each
  27.  
  28.     foreach($key in $subkeys){
  29.         $thisKey=$UninstallKey+"\\"+$key
  30.         $thisSubKey=$reg.OpenSubKey($thisKey)
  31.         $obj = New-Object PSObject
  32.         $obj | Add-Member -MemberType NoteProperty -Name "AppNumber" -Value $appNumber; $appNumber++;
  33.         $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
  34.         $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
  35.         $obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
  36.         $obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
  37.         $obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
  38.         $obj | Add-Member -MemberType NoteProperty -Name "UninstallString" -Value $($thisSubKey.GetValue("UninstallString"))
  39.         $InstalledList += $obj
  40.     }
  41.     foreach($key in $subkeys64){
  42.         $thisKey=$UninstallKey64+"\\"+$key
  43.         $thisSubKey=$reg.OpenSubKey($thisKey)
  44.         $obj = New-Object PSObject
  45.         $obj | Add-Member -MemberType NoteProperty -Name "AppNumber" -Value $appNumber; $appNumber++;
  46.         $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
  47.         $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
  48.         $obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
  49.         $obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
  50.         $obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
  51.         $obj | Add-Member -MemberType NoteProperty -Name "UninstallString" -Value $($thisSubKey.GetValue("UninstallString"))
  52.         $InstalledList += $obj
  53.     }
  54.  
  55. $apps = $InstalledList | Where-Object { $_.DisplayName -and $_.UninstallString -like "MsiExec*" } | select AppNumber, ComputerName, DisplayName, DisplayVersion, Publisher, UninstallString
  56.  
  57. $apps | Format-Table -AutoSize
  58.  
  59. $uninstallApp = Read-Host -Prompt "Select an App to uninstall" -Verbose
  60.  
  61. $UninstallStr =  ($apps | Where-Object {$_.appNumber -like $uninstallApp}).uninstallString
  62.  
  63. Invoke-Command -ComputerName $computerName -ScriptBlock {
  64.         $UninstallStr = $args[0]
  65.         $FullUninstallStr = $UninstallStr + ' /QN /L*V "C:\Temp\RemoteUninstall.log" REBOOT=R'
  66.  
  67.         ## Execute the software installer
  68.         Start-Process -FilePath "msiexec.exe" -ArgumentList $FullUninstallStr -Wait -Passthru
  69.  
  70.         } -ArgumentList $UninstallStr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement