使用 powershell 脚本卸载 google chrome
uninstall google chrome using powershell script
我正在使用以下代码从我的远程计算机上卸载 google chrome 软件,但是执行此脚本时没有任何输出。当我检查控制面板时,仍然存在 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()
}
您不应该使用 Win32_Product
WMI class,枚举操作的副作用之一是它会检查每个已安装程序的完整性,如果完整性检查失败则执行修复安装.
查询注册表以获取此信息更安全,它也恰好包含用于删除带有 msiexec
的产品的卸载字符串。此处的卸载字符串格式如 MsiExec.exe /X{PRODUCT_CODE_GUID}
,其中 PRODUCT_CODE_GUID
替换为该软件的实际产品代码。
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
}
顺便说一下,如果您已经知道给定程序的 MSI ProductCode,则不必通过这种方式查找卸载字符串。您可以简单地执行 msiexec /X{PRODUCT_CODE_GUID}
.
如果您还有其他不是由上述语法引起的问题,这将是一个操作问题,最好在 https://superuser.com 站点上进行故障排除。
编辑
通过我们的 chat 对话发现,您正在为每个用户安装 79.0.3945.130
版本。如果为每个用户安装,您可以使用以下命令删除每个用户 Chrome(如果版本不同,您将需要正确的版本路径):
&"C:\Users\username\AppData\Local\Google\Chrome\Application.0.3945.130\Installer\setup.exe" --uninstall --channel=stable --verbose-logging --force-uninstall
以后不建议在企业环境中使用ChromeSetup.exe
或ChromeStandaloneSetup64.exe
来安装和管理Chrome,应该改用Enterprise MSI并在系统范围内安装,以便您可以更有效地管理 Chrome。这是在企业环境中部署 Chrome 的受支持方式,我提供的脚本将用于通过 msiexec
卸载 Chrome 并在注册表中搜索所提供的 {PRODUCT_CODE}
.
假设它是一个 msi 安装并且远程 powershell 已启用:
invoke-command -computername comp001 { uninstall-package 'google chrome' }
对于程序提供者(所有用户),它类似于:
get-package *chrome* | % { $_.metadata['uninstallstring'] }
"C:\Program Files\Google\Chrome\Application.0.4638.54\Installer\setup.exe" --uninstall --channel=stable --system-level --verbose-logging
然后 运行 卸载字符串,但您必须找出静默卸载选项 (--force-uninstall)。它还 运行 在后台。
我正在使用以下代码从我的远程计算机上卸载 google chrome 软件,但是执行此脚本时没有任何输出。当我检查控制面板时,仍然存在 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()
}
您不应该使用 Win32_Product
WMI class,枚举操作的副作用之一是它会检查每个已安装程序的完整性,如果完整性检查失败则执行修复安装.
查询注册表以获取此信息更安全,它也恰好包含用于删除带有 msiexec
的产品的卸载字符串。此处的卸载字符串格式如 MsiExec.exe /X{PRODUCT_CODE_GUID}
,其中 PRODUCT_CODE_GUID
替换为该软件的实际产品代码。
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
}
顺便说一下,如果您已经知道给定程序的 MSI ProductCode,则不必通过这种方式查找卸载字符串。您可以简单地执行 msiexec /X{PRODUCT_CODE_GUID}
.
如果您还有其他不是由上述语法引起的问题,这将是一个操作问题,最好在 https://superuser.com 站点上进行故障排除。
编辑
通过我们的 chat 对话发现,您正在为每个用户安装 79.0.3945.130
版本。如果为每个用户安装,您可以使用以下命令删除每个用户 Chrome(如果版本不同,您将需要正确的版本路径):
&"C:\Users\username\AppData\Local\Google\Chrome\Application.0.3945.130\Installer\setup.exe" --uninstall --channel=stable --verbose-logging --force-uninstall
以后不建议在企业环境中使用ChromeSetup.exe
或ChromeStandaloneSetup64.exe
来安装和管理Chrome,应该改用Enterprise MSI并在系统范围内安装,以便您可以更有效地管理 Chrome。这是在企业环境中部署 Chrome 的受支持方式,我提供的脚本将用于通过 msiexec
卸载 Chrome 并在注册表中搜索所提供的 {PRODUCT_CODE}
.
假设它是一个 msi 安装并且远程 powershell 已启用:
invoke-command -computername comp001 { uninstall-package 'google chrome' }
对于程序提供者(所有用户),它类似于:
get-package *chrome* | % { $_.metadata['uninstallstring'] }
"C:\Program Files\Google\Chrome\Application.0.4638.54\Installer\setup.exe" --uninstall --channel=stable --system-level --verbose-logging
然后 运行 卸载字符串,但您必须找出静默卸载选项 (--force-uninstall)。它还 运行 在后台。