DOS Command Script: uninstall an application


The scenario is you have an application which was installed through MSI. And you only know the application name (in this case, the beginning part of the application name). Using msiexec command requires you to know the product GUID but the application might have different GUID for its different version. So here is the solution I came up with: quering the registry to get the list of product GUIDs (the format is always {GUID}), then match the DisplayName key with the application name.

 Here is a sample script which uninstalls applications whose names start with “Windows Live ID Sign-in Assistant”: 

for /f “tokens=7 delims=\” %%i in (‘reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall ^| FIND “{“‘) do (
  for /f “tokens=2,*” %%j in (‘reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\%%i /v DisplayName’) do (
    set dn=%%k
    if “!dn:~0,33!”==”Windows Live ID Sign-in Assistant” (
      echo Uninstalling Windows Live ID Sign-in Assistant
      msiexec /promptrestart /qb /x %%i /L+*v %TEMP%\wlidsvc.log
      if %ERRORLEVEL% NEQ 0 echo Uninstallation failed. Check the log %TEMP%\wlidsvc.log
    )
  )
)

That’s it and it should work.


Comments (3)

  1. alfsoft says:

    Hello! Thank you for your excellent script!

    Can you please explain the meaning of the ‘if "!dn:~0,33!"=="blah-blah"’ line?

    I mean I want to know more about such syntax, so I could apply it in another scripts.

  2. bill says:

    ~0,33 gives you the first 33 characters of the variable (in this case !dn!). So the purpose of that line is

    if dn.substring(0, 33) == "…."

    if you want to know more about how to manipulate substring with DOS, "set /?" gives you some decent help. Also "for /?" has some related info as well.

    "cmd /?" can tell you more about !variable!. Here’s the line I copied from the help:

    "If delayed environment variable expansion is enabled, then the exclamation character can be used to substitute the value of an environment variable at execution time."

    Hope that helps.

  3. Anonymous says:

    Thanks a lot.but i did not understand the step fully.