how to list all programs installed on windows using python

Solutions on MaxInterview for how to list all programs installed on windows using python by the best coders in the world

showing results for - "how to list all programs installed on windows using python"
Alondra
18 Sep 2019
1import winreg
2
3def foo(hive, flag):
4    aReg = winreg.ConnectRegistry(None, hive)
5    aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
6                          0, winreg.KEY_READ | flag)
7
8    count_subkey = winreg.QueryInfoKey(aKey)[0]
9
10    software_list = []
11
12    for i in range(count_subkey):
13        software = {}
14        try:
15            asubkey_name = winreg.EnumKey(aKey, i)
16            asubkey = winreg.OpenKey(aKey, asubkey_name)
17            software['name'] = winreg.QueryValueEx(asubkey, "DisplayName")[0]
18
19            try:
20                software['version'] = winreg.QueryValueEx(asubkey, "DisplayVersion")[0]
21            except EnvironmentError:
22                software['version'] = 'undefined'
23            try:
24                software['publisher'] = winreg.QueryValueEx(asubkey, "Publisher")[0]
25            except EnvironmentError:
26                software['publisher'] = 'undefined'
27            software_list.append(software)
28        except EnvironmentError:
29            continue
30
31    return software_list
32
33software_list = foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_32KEY) + foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_64KEY) + foo(winreg.HKEY_CURRENT_USER, 0)
34
35for software in software_list:
36    print('Name=%s, Version=%s, Publisher=%s' % (software['name'], software['version'], software['publisher']))
37print('Number of installed apps: %s' % len(software_list))