MikeWilliams_UK

Brute Force Removal [User]

Jun 7th, 2021 (edited)
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##########################
  2. # GetBackToSquareOne.ps1 #
  3. ##########################
  4.  
  5. CLS
  6.  
  7. ############################
  8. # MUST NOT BE RUN ELEVATED #
  9. ############################
  10.  
  11. $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
  12. if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
  13. {
  14.     Write-Host "╔══════════════════════════════════════════════════╗" -ForegroundColor Yellow
  15.     Write-Host "║ This script should not be run as Administrator ! ║" -ForegroundColor Yellow
  16.     Write-Host "╚══════════════════════════════════════════════════╝" -ForegroundColor Yellow
  17.  
  18.     Break Script
  19. }
  20.  
  21. # -----------------------------------------------
  22.  
  23. $delete = $true
  24.  
  25. Write-Host "Clearing Chem4Word user settings"
  26.  
  27. # Clear Settings @ HKEY_CURRENT_USER\Software\Chem4Word V3
  28.  
  29. $Key = "HKCU:\SOFTWARE\Chem4Word V3";
  30. $k = Get-ItemProperty -Path $key -ErrorAction SilentlyContinue
  31. if ($k -ne $null)
  32. {
  33.     Write-Host " Registry Key '$($Key)' found ..."
  34.     Write-Host "  Last Update Check: $($k.'Last Update Check')"
  35.     Write-Host "  Versions Behind: $($k.'Versions Behind')"
  36.  
  37.     if ($isIntalled -eq $false -and $delete -eq $true)
  38.     {
  39.         Write-Host "  Deleting '$($Key)' ..." -ForegroundColor Cyan
  40.         Remove-Item -Path $Key -Recurse
  41.     }
  42. }
  43. else
  44. {
  45.     # Write-Host "Registry Key '$($Key)' not found."
  46. }
  47.  
  48. # -----------------------------------------------
  49.  
  50. Write-Host "Clearing Chem4Word installation settings"
  51.  
  52. # Clear Word Add-In keys
  53. $baseKeys = @()
  54. $baseKeys += "HKCU:\SOFTWARE\Microsoft\Office\Word"
  55.  
  56. $keyNames = @()
  57. $keyNames += "Chemistry Add-in for Word"
  58. $keyNames += "Chem4Word"
  59. $keyNames += "Chem4Word V3"
  60. $keyNames += "Chem4Word.V3"
  61.  
  62. foreach ($baseKey in $baseKeys)
  63. {
  64.     foreach ($keyName in $keyNames)
  65.     {
  66.         $targetKey = "$($baseKey)\AddIns\$($keyName)"
  67.         $k = Get-ItemProperty -Path $targetKey -ErrorAction SilentlyContinue
  68.         if ($k -ne $null)
  69.         {
  70.             Write-Host " Registry Key '$($targetKey)' found ..."
  71.             Write-Host "  FriendlyName: $($k.'FriendlyName')"
  72.             Write-Host "  LoadBehavior: $($k.'LoadBehavior')"
  73.             Write-Host "  Manifest: $($k.'Manifest')"
  74.  
  75.             if ($isIntalled -eq $false -and $delete -eq $true)
  76.             {
  77.                 Write-Host "  Deleting '$($targetKey)' ..." -ForegroundColor Cyan
  78.                 Remove-Item -Path $targetKey -Recurse
  79.             }
  80.         }
  81.         else
  82.         {
  83.             # Write-Host "Registry Key '$($targetKey)' not found."
  84.         }
  85.     }
  86. }
  87.  
  88. foreach ($baseKey in $baseKeys)
  89. {
  90.     foreach ($keyName in $keyNames)
  91.     {
  92.         $targetKey = "$($baseKey)\AddInsData\$($keyName)"
  93.         $k = Get-ItemProperty -Path $targetKey -ErrorAction SilentlyContinue
  94.         if ($k -ne $null)
  95.         {
  96.             Write-Host " Registry Key '$($targetKey)' found ..."
  97.             Write-Host "  LoadCount: $($k.'LoadCount')"
  98.  
  99.             if ($isIntalled -eq $false -and $delete -eq $true)
  100.             {
  101.                 Write-Host "  Deleting '$($targetKey)' ..." -ForegroundColor Cyan
  102.                 Remove-Item -Path $targetKey -Recurse
  103.             }
  104.         }
  105.         else
  106.         {
  107.             # Write-Host "Registry Key '$($targetKey)' not found."
  108.         }
  109.     }
  110. }
  111.  
  112. # -----------------------------------------------
  113.  
  114. Write-Host "Clearing Microsoft Word disabled Add-Ins"
  115.  
  116. # HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Word\Resiliency\DisabledItems
  117. # HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\15.0\Word\Resiliency\DisabledItems
  118. # HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Resiliency\DisabledItems
  119.  
  120. for ($i=14; $i -le 16; $i++)
  121. {
  122.     $key = "HKCU:\SOFTWARE\Microsoft\Office\$($i).0\Word\Resiliency\DisabledItems";
  123.     $k = Get-ItemProperty -Path $key -ErrorAction SilentlyContinue
  124.     if ($k -ne $null)
  125.     {
  126.         Write-Host "Clearing $($Key)"
  127.         foreach ($kvp in $k.PSObject.Properties)
  128.         {
  129.             if (!$kvp.Name.StartsWith("PS"))
  130.             {
  131.                 if ($isIntalled -eq $false -and $delete -eq $true)
  132.                 {
  133.                     Write-Host "  Removing $($kvp.Name)" -ForegroundColor Cyan
  134.                     Remove-ItemProperty -Path $Key -Name $kvp.Name
  135.                 }
  136.             }
  137.         }
  138.     }
  139. }
  140.  
  141. # -----------------------------------------------
  142.  
  143. Write-Host "Clearing Chem4Word user data"
  144.  
  145. $folders = @()
  146. $folders += "$($env:ProgramData)\Chem4Word.V3"
  147. $folders += "$($env:USERPROFILE)\AppData\Local\Chem4Word.V3"
  148. $folders += "$($env:USERPROFILE)\AppData\Local\Chemistry Add-in for Word"
  149. $folders += "$($env:USERPROFILE)\AppData\Local\assembly\dl3"
  150.  
  151. foreach ($folder in $folders)
  152. {
  153.     if ($isIntalled -eq $false -and $delete -eq $true)
  154.     {
  155.         if (Test-Path $folder)
  156.         {
  157.             Write-Host "Deleting folder tree '$($folder)'"
  158.             Get-ChildItem -Path $folder -Recurse | Remove-Item -force -recurse
  159.             Remove-Item $folder
  160.         }
  161.     }
  162. }
Add Comment
Please, Sign In to add comment