Procedure to Uninstall the list of Programs from your endpoint

Please use the script to uninstall all of your blacklisted programs from your endpoint

Usage:

  • Modify the variable for your list of programs - blacklist=['program_name_1', 'program_name_2']
  • Run as System User
Condition: the blacklist container should have the similar name that how the program name looks on add/remove program list of the endpoint.

Limitation:
Programs can be removed:
Inventory can have msi and exe programs, all msi programs can be removed without user interaction but exe programs can be removed without user interaction when only the program has quiet uninstall string.
Programs can not be removed:
The exe programs which has no quiet uninstall string, those are not able to be removed without user interaction

blacklist=['Google Chrome', 'HandBrake 1.0.7', 'FileZilla Client 3.25.2', 'Microsoft Visual C++ 2015 x86 Additional Runtime - 14.0.24215', 'Acronis Backup Agent', 'Mozilla Firefox 53.0 (x86 en-US)', 'VLC media player']

import os
import _winreg

def ecmd(CMD):
    import ctypes
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    ret=OBJ.returncode
    return ret

def collectprograms(rtkey,pK,kA):
    list=[]
    try:        
        oK=_winreg.OpenKey(rtkey,pK,0,kA)
        i=0
        while True:
            try:
                bkey=_winreg.EnumKey(oK,i)
                vkey=os.path.join(pK,bkey)
                oK1=_winreg.OpenKey(rtkey,vkey,0,kA)
                try:
                    DN,bla=_winreg.QueryValueEx(oK1,'DisplayName')
                    DV,bla=_winreg.QueryValueEx(oK1,'DisplayVersion')
                    inlist=[DN.strip(), DV.strip()]                    
                    try:
                        IL, bla=_winreg.QueryValueEx(oK1,'InstallLocation')
                        if not IL:
                            IL='none'
                    except:
                        IL='none'
                    if IL:
                        inlist.append(IL.strip())
                    else:
                        inlist.append(IL)
                    try:
                        US,bla=_winreg.QueryValueEx(oK1,'UninstallString')
                    except:
                        US='none'
                    if US:
                        inlist.append(US.strip())
                    else:
                        inlist.append(US)
                    try:
                        QS,bla=_winreg.QueryValueEx(oK1,'QuietUninstallString')
                        if QS:
                            inlist.append(QS.strip())
                    except:
                        pass
                    list.append(inlist)
                    _winreg.CloseKey(oK1)
                except:
                    pass
                i+=1
            except:
                break
        _winreg.CloseKey(oK)
    except:
        pass
    return list

uninstallkey='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'
if 'PROGRAMFILES(X86)' in os.environ.keys():
    rklist=[(_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_WOW64_32KEY | _winreg.KEY_READ),
            (_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_WOW64_64KEY | _winreg.KEY_READ),
            (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_WOW64_32KEY | _winreg.KEY_READ),
            (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_WOW64_64KEY | _winreg.KEY_READ)]
else:
    rklist=[(_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_READ),
            (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_READ)]

ap=[]
for i in rklist:
    for j in collectprograms(i[0], i[1], i[2]):
        if j not in ap:
            ap.append(j)

ins, nf=[], []
for i in blacklist:
    for j in ap:
        if i==j[0]:
            if j not in ins:
                ins.append(j)

if ins:
    ins1=[a[0] for a in ins]
    for i in blacklist:
        if i not in ins1:
            nf.append(i)
    print 'Installed Blacklist-Programs and Status'
    for e in ins:
        if len(e)>4:
            rc=ecmd(e[-1])
            if rc==0:
                print '%s %s Successfully Removed'%(e[0], '.'*10)
            else:
                print '%s %s Error, Return Code: %s'%(e[0], '.'*10, str(rc))
        elif e[-1].startswith('MsiExec.exe'):
            cmd='%s %s'%(e[-1].replace('MsiExec.exe /I', 'MsiExec.exe /X'), '/qn')
            rc=ecmd(cmd)
            if rc==0:
                print '%s %s Successfully Removed'%(e[0], '.'*10)
            else:
                print '%s %s Error, Return Code: %s'%(e[0], '.'*10, str(rc))
        else:
            print '%s %s Require User Interaction'%(e[0], '.'*10)
    if nf:
        print '
Unavailable Blacklist-Programs from the Endpoint'
        for n in nf:
            print n
else:
    print 'No Blacklist-Program found on the Endpoint'

Sample Output:

Script in JSON:

20170621-Procedure-to-Uninstall-the-list-of-Programs-from-the-Windows-Endpoint.json (6.58 KB)

Script in JSON,

20170504-Uninstall-Blacklisted-Programs-Draft_1.json (7.93 KB)

When i run this for an application i get the following error, however the application is successfully removed.

Traceback (most recent call last): File “<string>”, line 128, in <module> NameError: name ‘cannotuninstall’ is not defined

Hi @Joners,

Thanks for the update

We will check and share the updated program as soon as possible

Hi, im getting the below when removing windows security essentials?
Traceback (most recent call last): File “<string>”, line 87, in <module> File “<string>”, line 23, in collectprograms WindowsError: [Error 2] The system cannot find the file specified

Hi @Joners and @dittoit,

Thank you for contacting us,

I have modified the script that would not throw error, I have checked almost all cases.

Please use it https://forum.mspconsortium.com/forum/script-library/11001-procedure-to-uninstall-the-list-of-programs-from-your-endpoint#post11001 and let us know your feedback.

Purushoth

All working, thanks.

Hi @dittoit,

Thank you for letting us know.

Hi @Joners,

Please let us know your feedback as well, We would like to help you if you have any problem on the script.

Thank you.