使用PowerShell脚本卸载谷歌浏览器

Posted

技术标签:

【中文标题】使用PowerShell脚本卸载谷歌浏览器【英文标题】:uninstall google chrome using powershell script 【发布时间】:2021-12-08 11:50:11 【问题描述】:

我正在使用以下代码从我的远程机器上卸载 google chrome 软件,但是这个脚本执行时没有输出。当我检查控制面板时,谷歌浏览器程序仍然存在。有人可以检查此代码吗?

foreach($computer in (Get-Content \path\to\the\file))

$temp1 = Get-WmiObject -Class Win32_Product -ComputerName $computer | where  $_.name -eq "Google Chrome"   
$temp1.Uninstall()

【问题讨论】:

【参考方案1】:

您不应该使用Win32_Product WMI 类,枚举操作的副作用之一是它会检查每个已安装程序的完整性,并在完整性检查失败时执行修复安装。


查询注册表以获取此信息更安全,该信息也恰好包含用于删除带有msiexec 的产品的卸载字符串。这里的卸载字符串将被格式化为MsiExec.exe /XPRODUCT_CODE_GUIDPRODUCT_CODE_GUID 替换为该软件的实际产品代码。

注意:此方法仅适用于使用 MSI 安装程序(或提取和安装 MSI 的设置可执行文件)安装的产品。对于不使用 MSI 安装的纯可执行安装程序,您需要查阅产品文档以了解如何卸载该软件,并找到另一种方法(例如众所周知的安装位置)来确定该软件是否已安装或不是。

注意 2: 我不确定这何时更改,但 ChromeSetup.exe 不再像以前那样包装 MSI。我已经修改了下面的代码来处理删除安装了 MSI 和 EXE 的 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


顺便说一句,如果您已经知道给定程序的 MSI ProductCode,则不必通过这种方式查找卸载字符串。你可以简单地执行msiexec /XPRODUCT_CODE_GUID

如果您还有其他不是由上述语法引起的问题,这将是一个操作问题,最好在https://superuser.com 站点进行故障排除。


编辑

通过我们的chat 对话发现,您正在按用户安装79.0.3945.130 版本。如果按用户安装,则可以使用以下命令删除按用户 Chrome(如果版本不同,则需要正确的版本路径):

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

以后在企业环境中不建议使用ChromeSetup.exeChromeStandaloneSetup64.exe来安装和管理Chrome,而应该使用Enterprise MSI在系统范围内安装,这样可以更有效地管理Chrome .这是在企业环境中部署 Chrome 的受支持方式,我提供的脚本可以通过 msiexec 卸载 Chrome 并在注册表中搜索提供的 PRODUCT_CODE

【讨论】:

我没有如此广泛地使用 powershell,因为我需要在 500 多个虚拟机中卸载,所以我尝试了这个。我不明白最后一行。你能完成吗? 这里是Invoke-Command 文档的Examples 部分的链接。您可以将上面的代码放在一个脚本块中并使用Invoke-Command 运行它。第一个示例显示了如何连接到远程计算机并执行此操作。注意:我建议像这样分块执行大规模删除;不要尝试一次完成所有 500 个。 @George 忘记在卸载命令中添加 /qn 选项。代码示例已更新。此参数抑制提示。您还可以使用 /qn 自动安装新软件。 @George 你为什么不和我一起in chat,我会尽力帮助你解决这个问题? @George 我已经用新版本更新了上面的脚本,该版本将使用所使用的任一安装方法读取注册表以了解 chrome 的存在,并相应地卸载。这里的最新版本也会等待 EXE 命令完成,之前的版本没有。【参考方案2】:

假设它是一个 msi 安装并且启用了远程 powershell:

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

对于程序提供者(所有用户),它类似于:

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

然后运行该卸载字符串,但您必须找出静默卸载选项(--force-uninstall)。它也在后台运行。

【讨论】:

OP 和我在聊天中确定他们使用的是非 msi 版本,所以这种方法行不通。另外,Uninstall-Package 可以与 MSI 安装的软件一起使用吗?

以上是关于使用PowerShell脚本卸载谷歌浏览器的主要内容,如果未能解决你的问题,请参考以下文章

怎样卸载google chrome浏览器

linux如何彻底卸载google浏览器

安卓谷歌浏览器打不开求原因

我的WIN8系统电脑 装了谷歌浏览器 为啥一点复制图片或者图片另存为 浏览器就崩溃

我的电脑为啥安装不了谷歌浏览器??

我的谷歌浏览器不能下载文件,提示“病毒扫描失败”。