SCCM SQL Query to Build Uninstaller Batch File

Share on:

I created the follow SQL query to create a batch file that uninstalls all known versions of a piece of software. Feel free to use and tweak as you see fit. You should build the query first to make sure you're only identifying the GUIDs for software you really want to uninstall. You'll need to setup SSMS to output as text instead of grid view, then copy and paste into a new batch file and trim the top and bottom a bit to remove the junk that comes with the output.

 1DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
 2
 3select 'ECHO Uninstalling ' + v_add_remove_programs.DisplayName0 + ' ' + v_add_remove_programs.Version0 + @NewLineChar + 'msiexec /x' + v_Add_Remove_Programs.ProdID0 + ' /qn REBOOT=REALLYSUPPRESS' + @NewLineChar + 'IF %ERRORLEVEL%==0 SET EMBRAVA=1' + @NewLineChar + 'IF %ERRORLEVEL%==3010 SET EMBRAVA=1' + @NewLineChar + @NewLineChar
 4  from v_Add_Remove_Programs
 5 where (v_Add_Remove_Programs.DisplayName0 like '%Blync%' or v_Add_Remove_Programs.DisplayName0 like '%Embrava%')
 6   and v_Add_Remove_Programs.ProdID0 like '{%}'
 7   and v_Add_Remove_Programs.DisplayName0 not in ('HSLBlyncLightLib','BlyncEnterpriseAdmin')
 8 group by v_add_remove_programs.DisplayName0
 9     , v_add_remove_programs.ProdID0
10	 , v_add_remove_programs.Version0
11 order by v_Add_Remove_Programs.DisplayName0, v_Add_Remove_Programs.Version0
12
13Here is some sample output:
14
15ECHO Uninstalling Blync 2.0.0
16msiexec /x{8DA962D4-2E5E-4D10-A0EB-841BB25607EE} /qn REBOOT=REALLYSUPPRESS
17IF %ERRORLEVEL%==0 SET EMBRAVA=1
18IF %ERRORLEVEL%==3010 SET EMBRAVA=1
19
20ECHO Uninstalling Blync 2.0.1
21msiexec /x{2BF5F559-3F7E-416E-87B6-BA9D7C92D408} /qn REBOOT=REALLYSUPPRESS
22IF %ERRORLEVEL%==0 SET EMBRAVA=1
23IF %ERRORLEVEL%==3010 SET EMBRAVA=1
24
25ECHO Uninstalling Blync 2.0.2
26msiexec /x{B9C84E9A-2802-4B04-904A-5019D138FC14} /qn REBOOT=REALLYSUPPRESS
27IF %ERRORLEVEL%==0 SET EMBRAVA=1
28IF %ERRORLEVEL%==3010 SET EMBRAVA=1

P.S. Obviously this is only going to work for Windows Installer uninstallations! :-)

Credit to this post on StackOverflow for the NewLineChar line.



1 comment