Raghav
Raghav

Reputation: 151

uninstall google chrome using powershell script

I am using the following code to uninstall google chrome software from my remote machines, But this script is executed with no output. And when i check the control panel, still the google chrome program exists. Can someone check this code?

foreach($computer in (Get-Content \path\to\the\file))
{
$temp1 = Get-WmiObject -Class Win32_Product -ComputerName $computer | where { $_.name -eq "Google Chrome"}   
$temp1.Uninstall()
}

Upvotes: 0

Views: 12222

Answers (2)

js2010
js2010

Reputation: 27423

Assuming it's an msi install and remote powershell is enabled:

invoke-command -computername comp001 { uninstall-package 'google chrome' } 

For the programs provider (all users), it's something like:

get-package *chrome* | % { $_.metadata['uninstallstring'] }

"C:\Program Files\Google\Chrome\Application\95.0.4638.54\Installer\setup.exe" --uninstall --channel=stable --system-level --verbose-logging

And then run that uninstallstring, but you'd have to figure out the silent uninstall option (--force-uninstall). It also runs in the background.

Upvotes: 0

codewario
codewario

Reputation: 21418

You shouldn't use the Win32_Product WMI class, one of the side effects of enumeration operations is it checks the integrity of each installed program and performs a repair installation if the integrity check fails.


It is safer to query the registry for this information instead, which also happens to contain the uninstall string for removing the product with msiexec. The uninstall string here will be formatted like MsiExec.exe /X{PRODUCT_CODE_GUID}, with PRODUCT_CODE_GUID replaced with the actual product code for that software.

Note: This approach will only work for products installed with an MSI installer (or setup executables which extract and install MSIs). For pure executable installers which make no use of MSI installation, you'll need to consult the product documentation for how to uninstall that software, and find another method (such as a well-known installation location) of identifying whether that software is installed or not.

Note 2: I'm not sure when this changed but ChromeSetup.exe no longer wraps an MSI as it used to. I have modified the code below to handle the removal of both the MSI-installed and EXE-installed versions of Chrome.

# We need to check both 32 and 64 bit registry paths
$regPaths = 
"HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

# Since technically you could have 32 and 64 bit versions of the same
# software, force $uninstallStrings to be an array to cover that case
# if this is reused elsewhere. Chrome usually should only have one or the
# other, however.
$productCodes = @( $regPaths | Foreach-Object {
    Get-ItemProperty "${_}\*" | Where-Object {
      $_.DisplayName -eq 'Google Chrome'
    }
  } ).PSPath

# Run the uninstall string (formatted like 
$productCodes | ForEach-Object {

  $keyName = ( Get-ItemProperty $_ ).PSChildName

  # GUID check (if the previous key was not a product code we'll need a different removal strategy)
  if ( $keyName -match '^{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}}$' ) {

    # Use Start-Process with -Wait to wait for PowerShell to finish
    # /qn suppresses the prompts for automation 
    $p = Start-Process -Wait msiexec -ArgumentList "/l*v ""$($pwd.FullName)/chromeuninst.log"" /X${keyName} /qn" -PassThru

    # 0 means success, but 1638 means uninstallation is pending a reboot to complete
    # This should still be considered a successful execution
    $acceptableExitCodes = 0, 1638
  }
  else {

    Write-Host "Stopping all running instances of chrome.exe, if any are running" 
    Get-Process chrome.exe -EA Ignore | Stop-Process -Force

    # The registry key still has an uninstall string
    # But cannot be silently removed
    # So we will have to get creating and control the uninstall window with PowerShell
    # We need to use the undocumented --force-uninstall parameter, added to below command
    $uninstallString = "$(( Get-ItemProperty $_).UninstallString )) --force-uninstall"

    # Break up the string into the executable and arguments so we can wait on it properly with Start-Process
    $firstQuoteIdx = $uninstallString.IndexOf('"')
    $secondQuoteIdx = $uninstallString.IndexOf('"', $firstQuoteIdx + 1)
    $setupExe = $uninstallString[$firstQuoteIdx..$secondQuoteIdx] -join ''
    $setupArgs = $uninstallString[( $secondQuoteIdx + 1 )..$uninstallString.Length] -join ''
    Write-Host "Uninstallation command: ${setupExe} ${setupArgs}"
    $p = Start-Process -Wait -FilePath $setupExe -ArgumentList $setupArgs -PassThru

    # My testing shows this exits on exit code 19 for success. However, this is undocumented
    # behavior so you may need to tweak the list of acceptable exit codes or remove this check
    # entirely.
    # 
    $acceptableExitCodes = 0, 19
  }

  if ( $p.ExitCode -notin $acceptableExitCodes ) {
    Write-Error "Program exited with $($p.ExitCode)"
    $p.Dispose()
    exit $p.ExitCode
  }

  exit 0
}

Incidentally, if you already know the MSI ProductCode of a given program you don't have to find the uninstall string this way. You can simply execute msiexec /X{PRODUCT_CODE_GUID}.

If you have further problems which aren't caused by the syntax of the above this would be an operational issue and would be better troubleshot over at the https://superuser.com site.


Edit

As discovered via our chat conversation, you are installing per user and with the 79.0.3945.130 version. You can remove per user Chrome with the following command if installed per user (if the version is different you will need the correct version path):

&"C:\Users\username\AppData\Local\Google\Chrome\Application\79.0.3945.130\Installer\setup.exe" --uninstall --channel=stable --verbose-logging --force-uninstall

In the future, it is not recommended to use ChromeSetup.exe or ChromeStandaloneSetup64.exe to install and manage Chrome in an enterprise environment, you should instead use the Enterprise MSI and install system-wide so you can manage Chrome more efficiently. This is the supported way to deploy Chrome in an enterprise environment, and the script I provided will work to uninstall Chrome via msiexec and searching the registry for the {PRODUCT_CODE} as provided.

Upvotes: 2

Related Questions