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

如何用Python卸载系统中所有应用(含非MSI安装程序)

用Python卸载Windows系统任意应用的解决方案

wmic命令只能识别通过Windows Installer(MSI)部署的程序,像UWP应用、部分EXE安装的软件、绿色软件这类不在它的识别范围内,这就是你看不到全部应用的原因。下面分场景给出可行的Python实现方案:

1. 卸载传统桌面应用(含EXE/MSI安装的程序)

大部分桌面应用会在Windows注册表中写入卸载信息,我们可以通过读取注册表获取卸载命令,再用Python执行:

代码示例:

import winreg
import subprocess
import sys

def get_uninstall_key(app_name):
    # 定义要查询的注册表路径
    reg_paths = [
        r"Software\Microsoft\Windows\CurrentVersion\Uninstall",
        r"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    ]
    
    for path in reg_paths:
        try:
            with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path) as key:
                # 遍历所有子项
                for i in range(winreg.QueryInfoKey(key)[0]):
                    subkey_name = winreg.EnumKey(key, i)
                    with winreg.OpenKey(key, subkey_name) as subkey:
                        display_name = winreg.QueryValueEx(subkey, "DisplayName")[0]
                        if app_name.lower() in display_name.lower():
                            uninstall_string = winreg.QueryValueEx(subkey, "UninstallString")[0]
                            return uninstall_string
        except Exception:
            continue
    return None

def uninstall_app(app_name):
    uninstall_cmd = get_uninstall_key(app_name)
    if not uninstall_cmd:
        print(f"未找到应用 {app_name} 的卸载信息")
        return
    
    # 处理带引号的命令
    if uninstall_cmd.startswith('"'):
        uninstall_cmd = uninstall_cmd[1:-1]
    
    try:
        subprocess.run(uninstall_cmd, check=True, shell=True)
        print(f"应用 {app_name} 卸载命令已执行")
    except subprocess.CalledProcessError as e:
        print(f"卸载失败:{e}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("请传入要卸载的应用名称")
        sys.exit(1)
    uninstall_app(sys.argv[1])

2. 卸载UWP应用(微软商店应用)

UWP应用需要用PowerShell命令管理,Python可以调用subprocess执行对应的PowerShell命令:

代码示例:

import subprocess

def uninstall_uwp_app(app_name):
    # 获取应用包名
    get_package_cmd = f'Get-AppxPackage | Where-Object {{$_.Name -like "*{app_name}*"}} | Select-Object -ExpandProperty PackageFullName'
    result = subprocess.run(["powershell", "-Command", get_package_cmd], capture_output=True, text=True)
    
    if not result.stdout.strip():
        print(f"未找到UWP应用 {app_name}")
        return
    
    package_full_name = result.stdout.strip()
    # 执行卸载命令
    uninstall_cmd = f'Remove-AppxPackage {package_full_name}'
    try:
        subprocess.run(["powershell", "-Command", uninstall_cmd], check=True)
        print(f"UWP应用 {app_name} 已卸载")
    except subprocess.CalledProcessError as e:
        print(f"卸载失败:{e}")

# 示例:卸载"Microsoft Edge"
uninstall_uwp_app("Microsoft Edge")

3. 注意事项

  • 管理员权限:卸载程序通常需要管理员权限,运行Python脚本时右键选择"以管理员身份运行"。
  • 绿色软件:这类软件没有卸载程序,直接删除安装文件夹即可,若需要清理残留,可以手动删除注册表中相关项(需谨慎操作)。
  • 静默卸载:部分应用的卸载命令支持/quiet或/silent参数,添加后可实现无交互卸载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 17:32:20