Tuesday, September 10, 2013

Uninstalling non-admin install of Google Chrome (Part II)

So my earlier attempt at uninstalling per-user / non-admin install of Google Chrome worked, but Google Updater was re-installing it daily.  So I re-wrote it in PowerShell mostly because I'm finally taking the plunge to learn it.  If you're using SCCM 2012 as I am, deploy the .bat file and use it as the command line.  It'll make sure execution policy doesn't get in your way.

--NOTE: I have NOT tested the MSI unistaller portion yet.  This is the way I imagine it'll work (get the uninstall string from the registry & run it, sounds simple).  But I haven't pulled doen the MSI and tested it yet.  When I do I'll update this section saying it's tested.

******************
* RemoveChrome.bat
******************
powershell set-executionpolicy unrestricted
Powershell .\ChromeKiller.ps1
powershell set-executionpolicy restricted

********End*******

******************
* ChromeKiller.ps1
* Beware word wrap
******************
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
# KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
# PURPOSE.

$ChromeType = "NONE"
$Log = New-Item -ItemType File -Path "C:\Temp\Chrome.log" -Force
$Error
$QueryString = Gwmi Win32_OperatingSystem
$QueryString = $QueryString.Caption

#Determine OS
Switch -Wildcard ($QueryString){
    "*Windows 7*"
        {$OS = "Win7"
         $UserDir = "C:\Users"
        }
    "*XP*" {
         $OS = "WinXP"
         $UserDir = "C:Documents and Settings"
        }
    }
 
Add-Content $Log "I'm on a $OS machine"

#Determine Install Type and kill it when found
 If(Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome') {
  $Uninstall = Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome' -Name "UninstallString"
  & $Uninstall
  $ChromeType = "Full"
  Add-Content $Log "User based with Admin Rights on a x64 machine"
  Add-Content $Log "Ran $uninstall"
 }
 Else {
 Add-Content $Log "Not x64 Non-MSI w/ Admin Rights Installed"
 }

 If(Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C534608B-4744-3856-AE7B-672DD3CDFEC7}') {
  $Uninstall = Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C534608B-4744-3856-AE7B-672DD3CDFEC7}' -Name "UninstallString"
  & $Uninstall
  $ChromeType = "Full"
  Add-Content $Log "MSI based with Admin Rights on a x64 machine"
  Add-Content $Log "Ran $uninstall"
 }
 Else {
 Add-Content $Log "Not x64 MSI Installed"
 }

  If(Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome') {
  $Uninstall = Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome' -Name "UninstallString"
  & $Uninstall
  $ChromeType = "Full"
  Add-Content $Log "User based with Admin Rights on a x32 machine"
  Add-Content $Log "Ran $uninstall"
 }
  Else {
 Add-Content $Log "Not x32 Non-MSI w/ Admin Rights Installed"
 }

 If(Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C534608B-4744-3856-AE7B-672DD3CDFEC7}') {
  $Uninstall = Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C534608B-4744-3856-AE7B-672DD3CDFEC7}' -Name "UninstallString"
  & $Uninstall
  $ChromeType = "Full"
  Add-Content $Log "MSI based with Admin Rights on a x32 machine"
  Add-Content $Log "Ran $uninstall"
 }
 Else {
 Add-Content $Log "Not x32 MSI Installed"
 }

#If it not a full install, check to see if it's lite and if so brute kill it
If ($ChromeType -eq "NONE") {
 Add-Content $Log "Wasn't installed with admin rights, brute forcing"
 
 $Folders = Get-ChildItem -Path $UserDir
 Add-Content $Log "Working on $Folders"
 foreach ($folder in $Folders) {
   Add-Content $Log "Working on $folder"
   If (Test-Path -path "$UserDir\$folder\Appdata\local\google\chrome") {

     #Remove Files
     Remove-Item -Path "$UserDir\$folder\Appdata\local\google\chrome" -Recurse -Force -ErrorAction SilentlyContinue
     Add-Content $Log "Found Chrome dir, removed $UserDir\$folder\Appdata\local\google\chrome"
    
     #Remove HKLM Files
     Add-Content $Log "Removing HKLM Files"
     Remove-Item -Path 'HKLM:\SOFTWARE\Classes\ChromeHTML' -ErrorAction SilentlyContinue -Recurse -Force
     $Keys = Get-ChildItem HKLM:\SOFTWARE\Clients\StartMenuInternet
     foreach ($Key in $Keys) {
       If($key.Name.Contains("Google")) {
         remove-item $key.PSPath -Recurse -Force -ErrorAction SilentlyContinue
       }
     }
     Remove-ItemProperty -Path HKLM:\SOFTWARE\Clients\StartMenuInternet -Name "Chrome.exe" -ErrorAction SilentlyContinue
     Remove-ItemProperty -Path HKLM:\SOFTWARE\RegisteredApplications -Name "Chrome" -ErrorAction SilentlyContinue
     Remove-Item "HKLM:\SOFTWARE\google\update\ClientStateMedium\{8A69D345-D564-463c-AFF1-A69D9E530F96}" -ErrorAction SilentlyContinue
     Remove-Item "HKLM:\SOFTWARE\Google\Update\Clients\{8A69D345-D564-463c-AFF1-A69D9E530F96}" -ErrorAction SilentlyContinue
     Remove-Item "HKLM:\SOFTWARE\Google\Update\ClientState\{8A69D345-D564-463c-AFF1-A69D9E530F96}" -ErrorAction SilentlyContinue
     Remove-Item "HKLM:\SOFTWARE\Wow6432Node\Google\Update\Clients\{8A69D345-D564-463c-AFF1-A69D9E530F96}" -ErrorAction SilentlyContinue

     #Remove HKU Files
     Add-Content $Log "Removing HKU Files"
     $Keys = Get-ChildItem registry::Hkey_Users
     foreach ($Key in $Keys) {
      switch -Wildcard ($Key.Name) {
        "HKEY_Users\S-1-5-18" {Add-Content $Log "Skipping Local Admin Hive"}
        "HKEY_Users\S-1-5-19" {Add-Content $Log "Skipping Guest Hive"}
        "HKEY_Users\S-1-5-20" {Add-Content $Log "Skipping Network Service Hive"}
        "HKEY_Users\.Default" {Add-Content $Log "Skipping Local Service Hive"}
        default {
         $TargetRoot = $key.PSPath
         Add-Content $Log "Working on $TargetRoot"
         $SubKeys = Get-ChildItem "$TargetRoot\SOFTWARE\Clients\StartMenuInternet" -ErrorAction SilentlyContinue
         foreach ($SubKey in $SubKeys)
          {If($Subkey.Name.Contains("Google"))
           {remove-item $Subkey.PSPath -Recurse -Force -ErrorAction SilentlyContinue}
          }
         Remove-ItemProperty -Path "$TargetRoot\SOFTWARE\Clients\StartMenuInternet" -Name "chrome.exe"-ErrorAction SilentlyContinue
         Remove-ItemProperty -Path "$TargetRoot\SOFTWARE\RegisteredApplications" -Name "chrome"-ErrorAction SilentlyContinue
         Remove-Item -Path "$TargetRoot\SOFTWARE\Classes\ChromeHTML" -ErrorAction SilentlyContinue -Recurse -Force
         Remove-Item -Path "$TargetRoot\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Chrome" -ErrorAction SilentlyContinue -Recurse -Force
         Remove-Item -Path "$TargetRoot\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" -ErrorAction SilentlyContinue -Recurse -Force
         Remove-Item -Path "$TargetRoot\Software\Google\Update\Clients\{8A69D345-D564-463c-AFF1-A69D9E530F96}" -ErrorAction SilentlyContinue -Recurse -Force
         Remove-Item -Path "$TargetRoot\Software\Google\Update\ClientState\{8A69D345-D564-463c-AFF1-A69D9E530F96}" -ErrorAction SilentlyContinue -Recurse -Force
         Remove-Item -Path "$TargetRoot\Software\Google\Update\Clients\{00058422-BABE-4310-9B8B-B8DEB5D0B68A}" -ErrorAction SilentlyContinue -Recurse -Force
         Remove-Item -Path "$TargetRoot\Software\Google\Update\ClientState\{00058422-BABE-4310-9B8B-B8DEB5D0B68A}" -ErrorAction SilentlyContinue -Recurse -Force
        }
      }
     }
    }
    Else {
      Add-Content $Log "Chrome not installed for $folder"
    }
 }
}
 Add-Content $Log "Complete"

********End*******

No comments:

Post a Comment