You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求可远程批量卸载用户电脑应用的PowerShell脚本

远程批量卸载应用的解决方案

一、为什么弃用Win32_Product

Win32_Product不仅无法识别未写入系统注册表的小型自装应用,还会触发MSI安装包的自动修复流程,可能导致系统不稳定或额外资源消耗,完全不适合批量卸载场景。

二、批量卸载已注册应用(覆盖绝大多数正规安装程序)

通过查询系统注册表获取卸载信息,比Win32_Product更高效且无副作用。以下是PowerShell远程卸载脚本:

# 填写目标电脑列表
$targetComputers = @("PC-001", "PC-002", "PC-003")
# 填写要卸载的应用名称关键词(支持模糊匹配)
$targetAppName = "XX工具"

Invoke-Command -ComputerName $targetComputers -ScriptBlock {
    # 同时查询64位和32位应用的注册表项
    $uninstallItems = Get-ItemProperty -Path @(
        "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
        "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
    ) | Where-Object { $_.DisplayName -like "*$using:targetAppName*" -and $null -ne $_.UninstallString }

    foreach ($item in $uninstallItems) {
        # 清理卸载命令中的引号
        $cleanUninstallCmd = $item.UninstallString -replace '^"|"$', ''
        # 适配MSI和EXE程序的静默卸载参数
        $silentArgs = if ($cleanUninstallCmd -match "msiexec") { "/qn /norestart" } else { "/s" }

        # 执行静默卸载
        Start-Process -FilePath $cleanUninstallCmd -ArgumentList $silentArgs -Wait -NoNewWindow
        Write-Host "完成 $($item.DisplayName) 的卸载尝试"
    }
}

脚本说明:

  • 自动适配64位/32位应用的注册表路径
  • 支持模糊匹配应用名称
  • 自动添加常见静默卸载参数,可根据目标应用调整(比如部分程序用/quiet)

三、卸载未注册的小型自装应用

这类程序无注册表项,需提前确认其安装路径、进程名及卸载程序(如uninst.exe),以下是针对性脚本:

$targetComputers = @("PC-001", "PC-002", "PC-003")
# 应用在目标电脑上的安装路径
$appInstallPath = "C:\Users\Public\MySmallApp"
# 卸载程序名称(无卸载程序则留空)
$uninstallExeName = "uninst.exe"

Invoke-Command -ComputerName $targetComputers -ScriptBlock {
    $localAppPath = $using:appInstallPath
    $localUninstallExe = Join-Path $localAppPath $using:uninstallExeName

    # 先终止应用进程(填写实际进程名)
    $runningProcess = Get-Process -Name "MySmallApp" -ErrorAction SilentlyContinue
    if ($runningProcess) {
        $runningProcess | Stop-Process -Force
        Write-Host "已终止应用进程"
    }

    # 优先执行卸载程序
    if (Test-Path $localUninstallExe) {
        Start-Process -FilePath $localUninstallExe -ArgumentList "/s" -Wait -NoNewWindow
        Write-Host "已执行静默卸载"
    } else {
        # 无卸载程序则直接删除应用目录
        Remove-Item -Path $localAppPath -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host "已删除应用安装目录"
    }
}

注意:

  • 需提前在单台测试机验证安装路径和进程名
  • 直接删除目录前务必确认无重要文件

四、前置条件

  • 执行脚本的账号需拥有所有目标电脑的管理员权限
  • 目标电脑已开启PowerShell远程(可通过Enable-PSRemoting -Force提前配置)
  • 确保目标电脑的防火墙允许WinRM连接

内容的提问来源于stack exchange,提问作者Domadea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:32:58