Last active
November 4, 2023 08:36
-
-
Save leidegre/94d8effefe3e55260c9527e0870dc4a8 to your computer and use it in GitHub Desktop.
Uninstall a bunch of apps by invoking the UninstallString for apps matching DisplayName (passed on command line)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires python 3.6 or later | |
# run as elevated user (requires admin permission for reg delete) | |
# python uninstall.py "<DisplayName>" | |
# you will get to confirm matching apps before any uninstall takes place | |
import sys | |
import winreg | |
import subprocess | |
def get_matching_apps(filter_string): | |
paths = [ | |
r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", | |
r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | |
] | |
matching_apps = [] | |
for path in paths: | |
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path) as key: | |
i = 0 | |
while True: | |
try: | |
subkey_name = winreg.EnumKey(key, i) | |
with winreg.OpenKey(key, subkey_name) as subkey: | |
try: | |
display_name = winreg.QueryValueEx(subkey, "DisplayName")[0] | |
if filter_string in display_name.lower(): | |
uninstall_cmd = winreg.QueryValueEx(subkey, "UninstallString")[0] | |
matching_apps.append((subkey_name, display_name, uninstall_cmd, path)) | |
except (FileNotFoundError, OSError): | |
# This specific subkey doesn't have a DisplayName or UninstallString | |
pass | |
except (FileNotFoundError, OSError): | |
# Reached the end of enumeration | |
break | |
i += 1 | |
matching_apps.sort(key=lambda x: x[1]) | |
return matching_apps | |
def main(): | |
if len(sys.argv) < 2: | |
print("Usage: script.py <filter_string>") | |
sys.exit(1) | |
filter_string = sys.argv[1].lower() | |
apps_to_uninstall = get_matching_apps(filter_string) | |
if not apps_to_uninstall: | |
print("No apps matched the filter.") | |
sys.exit(0) | |
print("Apps matching filter:") | |
for _, display_name, uninstall_cmd, _ in apps_to_uninstall: | |
print(" - ", display_name) | |
print(" ", uninstall_cmd) | |
choice = input("Do you want to uninstall all the above apps? [Y/N]: ") | |
if choice.lower() != 'y': | |
sys.exit(0) | |
for subkey_name, _, uninstall_cmd, path in apps_to_uninstall: | |
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path, 0, winreg.KEY_ALL_ACCESS) as key: | |
print(f"Uninstalling {subkey_name}...") | |
subprocess.run(uninstall_cmd, shell=True) | |
winreg.DeleteKey(key, subkey_name) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment