[Python] How to list all app/software/programs installed on Windows 11 using a Python script

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I develop a Python script to list all installed apps in my Windows 11 system. I would like to replicate the same results displayed in Settings > App > Installed Apps. Optionally, I would like to list apps installed not only for my user but in the entire system. Surfing the web, I found these four commands:

    cmd1 = "Get-WmiObject -Class Win32_Product | Select-Object Name, Version, Architecture | Sort-Object Name"

    cmd2 = "Get-AppxPackage | Select-Object Name, Version, Architecture | Sort-Object Name"

    path = f"HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*"
    cmd3 = f"Get-ItemProperty -Path {path} | Select-Object DisplayName, DisplayVersion, Architecture | Sort-Object Name"

    path = f"HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*"
    cmd4 = f"Get-ItemProperty -Path {path} | Select-Object DisplayName, DisplayVersion, Architecture | Sort-Object Name"


    I develop a simple code to run these four commands sequentially:

    def list_installed_apps(cmd: str):
    # Run the PowerShell command and capture output
    result = subprocess.run(["powershell", "-Command", cmd], capture_output=True, text=True)
    # Split the result into lines and filter out unwanted lines
    installed_apps = result.stdout.splitlines()
    return installed_apps

    ##############################
    # CMD 1
    ##############################
    cmd = "Get-WmiObject -Class Win32_Product | Select-Object Name, Version, Architecture | Sort-Object Name"
    apps_1 = list_installed_apps(cmd)

    # Find the index of the header line and column label boundaries
    header_index = [i for i, app in enumerate(apps_1) if app.find("Version") != -1][0]
    header = apps_1[header_index]
    bound_1 = header.find("Version")
    bound_2 = header.find("Architecture")

    # Remove the header and any empty lines
    apps_1 = [app.strip() for app in apps_1 if app.strip() and "Name" not in app]
    apps_1 = apps_1[1:] #removes the line of -------

    apps_1_ordered = [
    {"name": app[:bound_1], "version": app[bound_1:bound_2], "architecture": app[bound_2:]}
    for app in apps_1
    ]


    ##############################
    # CMD 2
    ##############################
    cmd = "Get-AppxPackage | Select-Object Name, Version, Architecture | Sort-Object Name"
    apps_2 = list_installed_apps(cmd)

    # Find the index of the header line and column label boundaries
    header_index = [i for i, app in enumerate(apps_2) if app.find("Version") != -1][0]
    header = apps_2[header_index]
    bound_1 = header.find("Version")
    bound_2 = header.find("Architecture")

    # Remove the header and any empty lines
    apps_2 = [app.strip() for app in apps_2 if app.strip() and "Name" not in app]
    apps_2 = apps_2[1:] #removes the line of -------

    apps_2_ordered = [
    {"name": app[:bound_1], "version": app[bound_1:bound_2], "architecture": app[bound_2:]}
    for app in apps_2
    ]


    ##############################
    # CMD 3
    ##############################
    path = f"HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*"
    cmd = f"Get-ItemProperty -Path {path} | Select-Object DisplayName, DisplayVersion, Architecture | Sort-Object Name"
    apps_3 = list_installed_apps(cmd)

    # Find the index of the header line and column label boundaries
    header_index = [i for i, app in enumerate(apps_3) if app.find("DisplayVersion") != -1][0]
    header = apps_3[header_index]
    bound_1 = header.find("DisplayVersion")
    bound_2 = header.find("Architecture")

    # Remove the header and any empty lines
    apps_3 = [app.strip() for app in apps_3 if app.strip() and "DisplayName" not in app]
    apps_3 = apps_3[1:] #removes the line of -------

    apps_3_ordered = [
    {"name": app[:bound_1], "version": app[bound_1:bound_2], "architecture": app[bound_2:]}
    for app in apps_3
    ]


    ##############################
    # CMD 4
    ##############################
    path = f"HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*"
    cmd = f"Get-ItemProperty -Path {path} | Select-Object DisplayName, DisplayVersion, Architecture | Sort-Object Name"
    apps_4 = list_installed_apps(cmd)

    # Find the index of the header line and column label boundaries
    header = apps_4[1]
    bound_1 = header.find("DisplayVersion")
    bound_2 = header.find("Architecture")

    # Remove the header and any empty lines
    apps_4 = [app.strip() for app in apps_4 if app.strip() and "DisplayName" not in app]
    apps_4 = apps_4[1:] #removes the line of -------

    apps_4_ordered = [
    {"name": app[:bound_1], "version": app[bound_1:bound_2], "architecture": app[bound_2:]}
    for app in apps_4
    ]


    ##############################
    # SUM RESULTS OF ALL COMMANDS AND SAVE
    ##############################
    all_apps = apps_1_ordered + apps_2_ordered + apps_3_ordered + apps_4_ordered
    fileName = "" # SET YOUR NAME
    with open(fileName, "w") as f:
    for app in all_apps:
    f.write(f"{app['name']} --- {app['version']} --- {app['architecture']}\n")


    Unfortunately, this file content doesn't completely match the apps sorted in the settings. How can I obtain that list?

    Continue reading...

Compartilhe esta Página