Uninstall MSI App - Delete Registry Key if 1605

I want to delete HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall{GUID} if executing its UninstallString results in an exit code 1605 (application is not installed).

I can do this if I were uninstalling a single application and knew its GUID, but I’m using a script meant to remove any version of the product. My first thought is to copy Remove-MsiApplications and modify it to do what I want - I’m ASSUMING I can get it to do what I want.

Before I do that, does anyone else have a better idea?

If you’re interested in why:
I have been tasked with removing all versions of all products from a vendor. There are 94 product/version combinations. I have run my script against a pilot group and I’ve run into a large percentage of those that had a 1605 error uninstalling a specific product (versions may/may not vary). I am not the first to try this. I’m guessing that whatever was done before left these registry keys orphaned.

Before I unleash this on the remaining 3,000 computers that have at least one of these products installed, I’d like a solution that is dynamic and not have to add problem GUIDs to my script as they crop up…because this needs to be done in the next 6 business days.

Actually, it proved not to be too difficult to create a list of GUIDS (I already had the info in a spreadsheet.)

[hashtable]$GUIDS = @{  'Box_Tools_3.2.8.1512'      = '{0DB78379-24E4-40B4-91AA-9A463C618F80}';
                        'Box_Tools_3.3.3.1728'      = '{4DD12C59-0CD4-422F-9DF3-C7A5E10E6539}';
                        'Box_Tools_3.4.5.371'       = '{0E759E4C-8D0F-4006-8237-07B779426D3D}';
                        'Box_Tools_4.3.4.475'       = '{1A7BDFC2-66DF-409B-B809-7FD26C003960}';
                        'Box_Tools_4.4.2.522'       = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_4.5.1.537'       = '{88F6F5AC-2BE0-48B1-92A4-3AFE0F069B0B}';
                        'Box_Tools_4.6.0.647'       = '{40C2C208-B248-4294-A8E2-67948697E7F0}';
                        'Box_Tools_4.6.1.654'       = '{1A7BDFC2-66DF-409B-B809-7FD26C003960}';
                        'Box_Tools_3.2.10.1533'     = '{56647361-687B-452B-8999-6179125FFD63}';
                        'Box_Tools_3.2.7.1497'      = '{6E83E7B5-7BF7-4223-899F-F86CE0E5E178}';
                        'Box_Tools_3.3.1.1713'      = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_3.5.2.382'       = '{BAD2F3F3-2685-4D4F-A944-713918C20BEE}';
                        'Box_Tools_3.5.3.383'       = '{01D7374B-5CAE-4C7C-AAF6-E7C7E2CA2DAB}';
                        'Box_Tools_4.4.1.508'       = '{F2D33DC7-8EC3-46DF-B294-8A02A460056A}';
                        'Box_Tools_4.5.3.571'       = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_4.9.1.720'       = '{1A7BDFC2-66DF-409B-B809-7FD26C003960}';
                        'Box_Tools_3.1.15.1312'     = '{3A5701D3-F860-4470-8840-5C2FFBEFA30E}';
                        'Box_Tools_3.4.3.359'       = '{15413625-D5D7-4C1F-83CB-44610CABDDA0}';
                        'Box_Tools_4.10.3.724'      = '{0C3B6637-E0DF-4125-AC1F-632F546C9DE3}';
                        'Box_Tools_4.11.0.822'      = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_4.2.0.453'       = '{5AA8C882-4FDE-4097-B62F-07B8B4438AE3}';
                        'Box_Tools_4.5.0.528'       = '{B78331C6-8897-4F0C-BD95-099CC40A61F5}';
                        'Box_Tools_4.5.2.548'       = '{B56B04EF-232F-458F-B0B0-3D56352DC91B}';
                        'Box_Tools_4.5.6.593'       = '{EE4ACDBD-AF65-4A79-8D63-89F359AAC285}';
                        'Box_Tools_4.5.7.609'       = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_3.1.16.1313'     = '{A7055A07-0E81-4A78-ABA8-5849A31BDBA1}';
                        'Box_Tools_3.2.13.1660'     = '{00FEE73D-E368-46A8-B4E1-475050722328}';
                        'Box_Tools_4.0.10.415'      = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_4.1.9.440'       = '{01D7374B-5CAE-4C7C-AAF6-E7C7E2CA2DAB}';
                        'Box_Tools_4.10.2.808'      = '{193EEF8F-283B-478E-B9B7-5C0CCCEB7EC8}';
                        'Box_Tools_4.3.2.469'       = '{2DE620BB-A1B6-4123-8A97-CFBF63BD669B}';
                        'Box_Tools_4.3.6.482'       = '{0C3B6637-E0DF-4125-AC1F-632F546C9DE3}';
                        'Box_Tools_4.5.4.577'       = '{E2108CBF-0134-4FD2-928E-4A6931C9D4E3}';
                        'Box_Tools_4.9.2.722'       = '{9880B9BC-A92A-4EBF-AEE8-E260C418C4A0}';
                        'Box_1.17.120'              = '{70BE6D52-C4F9-4C00-842E-9966AC42B8E9}';
                        'Box_2.13.518'              = '{6596E7BF-68D9-4E97-9EE1-83F6B16EC397}';
                        'Box_2.15.121'              = '{D6BE74CB-634E-4F71-B1F2-25BFAE72C5F5}';
                        'Box_2.8.207'               = '{CF500C01-123E-4AB6-8F8F-CF08A2DCF28E}';
                        'Box_2.10.219'              = '{964586E2-6402-41F6-A28A-DD8660A71A28}';
                        'Box_2.9.369'               = '{6BAE18AE-D398-4D2C-84DB-B84A375C6C8E}';
                        'Box_2.1.107'               = '{B3130D2C-1378-4B53-BB23-E5EBA2395AA7}';
                        'Box_2.14.377'              = '{601D96E8-8C7B-47A9-A2BD-B1289B1882D3}';
                        'Box_2.16.298'              = '{BA61B4A3-FCB0-4E89-8D55-21DB0A409DB4}';
                        'Box_2.17.176'              = '{4CAE7B4D-6F29-4BE1-97BB-2A007B602AA6}';
                        'Box_2.11.46'               = '{5D1B7443-8D37-4125-AE30-0E8CCCE11424}';
                        'Box_2.14.378'              = '{A4B27B86-FC4E-4F04-81D4-6371A85F5381}';
                        'Box_2.2.167'               = '{E077F6AC-D853-4AA7-BDAB-7C136CF2502C}';
                        'Box_2.6.186'               = '{05089BF1-66FB-4207-A072-232399F233F2}';
                        'Box_Edit_1.1.44'           = '{0AEBC85C-11F1-4106-8A14-AF3B7FFD9357}';
                        'Box_for_Office_3.7.6'      = '{369638CE-586A-4156-96F6-572F7DA07826}';
                        'Box_for_Office_4.8.1287.0' = '{BCEECB83-6E20-4670-A367-4E1013D2848D}';
                        'Box_for_Office_4.1.1102.0' = '{ED478868-8308-49F2-8309-BFD468F9C6CF}';
                        'Box_for_Office_4.1.1104.0' = '{C37EB6A2-47D0-43EE-AB45-D81E498DA2A5}';
                        'Box_for_Office_4.9.1301.0' = '{CAF47FCF-A975-4135-AA0F-F11A214BF6E6}';
                        'Box_for_Office_4.6.1251.0' = '{0387D998-D11F-4D40-86F0-99440DAB011F}';
                        'Box_Sync_4.0.7965.0'       = '{DCD2FEBB-93F3-482C-9887-DD2DC9A40992}';
                        'Box_Sync_4.0.7848.0'       = '{0653E263-C86D-44AB-AE83-25407370FCE1}';
                        'Box_Sync_4.0.7911.0'       = '{BFA57077-F78C-4B92-815E-7BCDA6B9686E}';
                        'Box_Sync_4.0.5964.0'       = '{928E1F55-8D86-4655-9960-1A92E01FD544}';
                        'Box_Sync_4.0.7318.0'       = '{C23C4679-DCB8-40E8-86BD-DB990A3599AD}';
                        'Box_Sync_4.0.7929.0'       = '{CD5CB679-159B-4E4C-B847-B29B492D106E}';
                        'Box_Sync_4.0.7076.0'       = '{3FD3A263-0240-4AD1-A5FD-F66D745A454C}';
                        'Box_Sync_4.0.7800.0'       = '{CB732C5D-1F06-41A8-B984-B84B87053E8C}';
                        'Box_Sync_4.0.7992.0'       = '{D69E0328-B93E-48D0-A9D6-6973200C16FA}';
                        'Box_Sync_4.0.8004.0'       = '{5E8758DE-4FAB-494D-97FF-A09D2165D973}';
                        'Box_Sync_4.0.8009.0'       = '{77C79299-C0AD-4054-A2C6-10202746A5D8}'            
                    }
            
Write-Log "Processing known GUIDs."
ForEach ($Key in $GUIDs.Keys) {
    $GUID = $GUIDS[$Key]
    Write-Log -Message "Processing $GUID"
    $Result = (Execute-Msi -Action Uninstall -Path $GUID -PassThru).ExitCode ## Box Tools 4.11.0.822
    If ($Result -eq 1605) {
        Remove-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$GUID"
        Remove-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\$GUID"
    }
}

You could of used Get-InstalledApplication to check if the application was installed and return the GUID of the MSI if it was present. Or you could search through the uninstall key for Box_Tools and return the GUID that way.