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

如何用Python脚本获取Windows 11中与「设置>应用>已安装应用」完全匹配的全系统已安装应用列表

如何用Python脚本获取Windows 11中与「设置>应用>已安装应用」完全匹配的全系统已安装应用列表

我明白你想要让Python脚本获取的应用列表和Windows 11「设置>应用>已安装应用」完全一致的需求——这个列表其实整合了桌面应用、UWP应用、用户专属应用、系统级应用等多种类型,你之前用的四个命令各自只能覆盖一部分场景,还存在遗漏、重复甚至潜在风险(比如Win32_Product可能触发应用修复),接下来我给你梳理问题所在,以及更精准的实现方案:

先说说你之前方法的问题

  • Get-WmiObject -Class Win32_Product:这个命令只追踪通过Windows Installer(.msi)安装的应用,覆盖范围极小,而且运行时可能会自动触发已安装应用的修复操作,非常不推荐使用。
  • Get-AppxPackage:默认仅获取当前用户的UWP应用,无法拿到系统级或其他用户安装的UWP应用。
  • 注册表路径仅覆盖了HKLM下的系统级应用,漏掉了**HKCU(当前用户专属)**的Uninstall注册表项,这部分是设置里显示的「仅当前用户」应用的重要来源。

优化后的实现方案

下面的脚本会完整覆盖「设置>已安装应用」的所有数据源,同时处理重复项、统一输出格式:

import subprocess
import json
from collections import OrderedDict

def get_powershell_output(cmd):
    # 运行PowerShell命令,用JSON格式输出方便解析(比处理文本表格靠谱多了)
    result = subprocess.run(
        ["powershell", "-Command", f"{cmd} | ConvertTo-Json -Depth 10"],
        capture_output=True,
        text=True,
        encoding='utf-8'
    )
    if result.returncode != 0:
        print(f"命令执行出错: {result.stderr}")
        return []
    try:
        return json.loads(result.stdout)
    except json.JSONDecodeError:
        return []

def get_all_installed_apps():
    all_apps = []

    # 1. 获取全系统+当前用户的UWP应用
    appx_cmd = """
    Get-AppxPackage -AllUsers | Select-Object Name, Version, Architecture, @{Name='InstallLocation'; Expression={$_.InstallLocation}}, @{Name='UserScope'; Expression={'All Users'}}
    Get-AppxPackage | Select-Object Name, Version, Architecture, @{Name='InstallLocation'; Expression={$_.InstallLocation}}, @{Name='UserScope'; Expression={'Current User'}}
    """
    appx_apps = get_powershell_output(appx_cmd)
    for app in appx_apps:
        if app.get("Name"):  # 过滤空名称的应用
            all_apps.append({
                "name": app["Name"],
                "version": app.get("Version", ""),
                "architecture": app.get("Architecture", ""),
                "scope": app.get("UserScope", "")
            })

    # 2. 获取注册表中的桌面应用(系统级+用户级,32位+64位)
    registry_paths = [
        r"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
        r"HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
        r"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
        r"HKCU:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
    ]
    for path in registry_paths:
        reg_cmd = f"""
        Get-ItemProperty -Path "{path}" | Where-Object {{ $_.DisplayName -ne $null }} | Select-Object DisplayName, DisplayVersion, Architecture, @{{Name='UserScope'; Expression={{ if ($_.PSPath -match 'HKCU') {{ 'Current User' }} else {{ 'All Users' }} }} }}
        """
        reg_apps = get_powershell_output(reg_cmd)
        for app in reg_apps:
            if app.get("DisplayName"):
                all_apps.append({
                    "name": app["DisplayName"],
                    "version": app.get("DisplayVersion", ""),
                    "architecture": app.get("Architecture", ""),
                    "scope": app.get("UserScope", "")
                })

    # 3. 去重:根据应用名称+版本+范围去重
    unique_apps = OrderedDict()
    for app in all_apps:
        key = f"{app['name']}_{app['version']}_{app['scope']}"
        if key not in unique_apps:
            unique_apps[key] = app

    return list(unique_apps.values())

# 执行并保存结果
if __name__ == "__main__":
    installed_apps = get_all_installed_apps()
    # 保存为易读的文本格式
    with open("installed_apps.txt", "w", encoding="utf-8") as f:
        f.write("应用名称 --- 版本 --- 架构 --- 安装范围\n")
        f.write("-----------------------------------------\n")
        for app in sorted(installed_apps, key=lambda x: x["name"]):
            f.write(f"{app['name']} --- {app['version']} --- {app['architecture']} --- {app['scope']}\n")
    print(f"已成功导出{len(installed_apps)}个已安装应用到installed_apps.txt")

关键优化点说明

  • 改用ConvertTo-Json输出:相比解析PowerShell的文本表格,JSON格式更稳定,不会因为列对齐问题导致解析错误。
  • 覆盖所有数据源:包含了UWP应用的-AllUsers参数,以及HKCU下的用户专属注册表项,完全匹配设置里的应用范围。
  • 去重处理:避免同一个应用在多个数据源中重复出现(比如某些UWP应用可能同时存在于系统和用户目录)。
  • 移除了Win32_Product:用注册表方式替代,更安全且覆盖范围更广。

额外提示

如果想要完全和设置里的排序一致,只需要在最后保存时按照「应用名称」排序即可,上面的脚本已经做了这个处理。

备注:内容来源于stack exchange,提问作者eljamba

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 09:52:58