Advertisement
armyf35

Intune app detection/uninstall

Nov 3rd, 2020
2,939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $appname = "Google Chrome"
  2.  
  3. $paths = @(
  4.     'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
  5.     'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
  6. );
  7.  
  8. $tempPaths = @();
  9. foreach ($path in $paths) {
  10.     if (Test-Path -Path $path) {
  11.         $tempPaths += $path;
  12.     };
  13. };
  14. $paths = $tempPaths;
  15.  
  16. $app = Get-ItemProperty $paths | Where-Object DisplayName -eq $appname | Select-Object -first 1;
  17.  
  18. if ($null -ne $app) {
  19.     Write-Output "Found $appname in installed software.";
  20.     $app;
  21. } else {
  22.     Throw "$appname not found";
  23. };
  24.  
  25. # End Detection
  26. # Start Uninstall
  27.  
  28. if ($null -ne $app) {
  29.     $app;
  30.     Write-Host "Starting appname uninstall";
  31.  
  32.     $guid = $app.PSChildName.trim("{}");
  33.     $hasGuid = $false;
  34.  
  35.     Try {
  36.         [System.Guid]::Parse($guid);
  37.         $hasGuid = $true;
  38.     } Catch {
  39.         Write-Host "Invalid GUID: $($app.PSChildName)";
  40.         $hasGuid = $false;
  41.     };
  42.  
  43.     if ($hasGuid) {
  44.         Write-Host "Running command: msiexec /x $($app.PSChildName) /qn";
  45.         $proc = Start-Process -FilePath msiexec -Wait -PassThru -ArgumentList "/x", $app.PSChildName, "/qn";
  46.     } else {
  47.         Write-Host "Found uninstall string";
  48.  
  49.         $uninstallString = $app.UninstallString.split('"')[1];
  50.         $arguments = $app.UninstallString.split('"')[2].split(" ");
  51.         $arguments += "--force-uninstall";
  52.         $arguments = $arguments[1..($arguments.Length - 1)];
  53.  
  54.         Write-Host "Running command: $uninstallString $arguments";
  55.  
  56.         $proc = Start-Process -FilePath $uninstallString -Wait -PassThru -ArgumentList $arguments;
  57.     };
  58.    
  59.     Write-Host "Uninstall exited with code: $($proc.ExitCode)";
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement