Friday, January 15, 2010

Uninstall products from the command line

Uninstalling products from the command line is tricky when there is no shortcut to parse. So we need to go to the registry and pull out information of where the program exists in there, and the proper product code (a GUID). that's unique to every system. To do this I have employed the mightiest of tools, egrep.

You'll need to find egrep for Windows. It's getting harder and harder to find.

Here's a script I made to uninstall a program called "Driver and Utility".

reg query
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products
/s > "%TEMP%\reguninstall.txt"
TYPE "%TEMP%\reguninstall.txt"
\\myk\netlogon\grep.exe -B 15 -E "Driver & Utility" \\myk\netlogon\grep.exe -o -E
"UninstallString.+"
\\myk\netlogon\grep.exe -o -E "MsiExec.+" > "%temp%\uninstallcmd.txt"
FOR /F "tokens=1-10 delims={" %A IN ('TYPE "%temp%\uninstallcmd.txt"') DO
msiexec.exe /x {%B /qb

You'll have to excuse the line cropping. This script does 3 things.

#1, it pulls every program option from the registry and saves it in a file
#2, it then greps for the program name. This needs to be specific. Using grep we can ask it to pull lines previous to finding the query. We pull back 15 lines because it's a big key and we need to find the GUID
#3, we make a variable out of the GUID and execute the uninstall command.

The final uninstall command should look like this:

msiexec.exe /x {48435D4A-BDAF-4AC3-B172-B25F1AADE6C6} /qb

Voila.