Chrome静默卸载PowerShell脚本问题:如何正确调用$LocalVersion变量
Chrome静默卸载脚本修复方案
问题根源
原脚本里$LocalVersion的赋值逻辑出错:你先通过$_.FullName获取了setup.exe的完整路径,又额外加了.name,导致变量最后只存储了setup.exe这个文件名,而非完整路径,Start-Process自然找不到要执行的文件。
解决步骤
修正变量赋值逻辑
直接获取setup.exe的完整路径,不要额外提取文件名:# 获取Chrome卸载用的setup.exe完整路径 $ChromeSetupPath = Get-ChildItem -Path "C:\Program Files\Google\Chrome\Application" -Filter "setup.exe" -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -ExpandProperty FullName -First 1加
-First 1是避免找到多个setup.exe(不同版本目录下可能存在)时出现歧义,优先取第一个匹配的文件。增加存在性判断
避免找不到文件时执行Start-Process报错,先检查变量是否有值:if ($ChromeSetupPath) { Start-Process -FilePath $ChromeSetupPath -ArgumentList '--uninstall --system-level --multi-install --force-uninstall' -Wait -Verbose Write-Host "Chrome卸载命令已执行" } else { Write-Warning "未找到Chrome的setup.exe卸载程序" }额外加
-Wait参数可以让脚本等待卸载完成再继续,比固定Start-Sleep 10更可靠。
完整修正后的脚本
# 静默卸载Chrome $ChromeSetupPath = Get-ChildItem -Path "C:\Program Files\Google\Chrome\Application" -Filter "setup.exe" -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -ExpandProperty FullName -First 1 if ($ChromeSetupPath) { # 执行静默卸载,等待卸载完成 Start-Process -FilePath $ChromeSetupPath -ArgumentList '--uninstall --system-level --multi-install --force-uninstall' -Wait -Verbose Write-Host "Chrome卸载流程已完成" } else { Write-Warning "未定位到Chrome卸载程序,请检查Chrome安装路径是否正确" }
内容的提问来源于stack exchange,提问作者Justus Feldhaus
相关产品推荐
相关产品推荐

