batch file to uninstall any version of a software

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JrSysEngineer
Posts: 5
Joined: 22 Mar 2012 10:34

batch file to uninstall any version of a software

#1 Post by JrSysEngineer » 22 Mar 2012 11:04

Hello,

I am trying to write a batch file that will query the registry to find and uninstall any version of Java. I would like to make it as efficient as possible so that it can be used in an enterprise situation. I would also like it to be able to be used regardless of the version of Java is installed currently on the computers so that it will be able to be used well into the future. I have thus far used registry queries to search for each version, from Java 6 Update 14 to Java 7 Update 3 in both the x86 and x64 platforms (Windows XP, 7, Server 2K8 and 2K3).

Code: Select all

ECHO OFF
CLS


ECHO. *******************************************************
ECHO. *******************************************************
ECHO.
ECHO.              DO NOT CLOSE THIS WINDOW
ECHO.
ECHO. Installing Java 7 Update 3
ECHO.
ECHO. Do not close this window. This window will close
ECHO. automatically when the installation is complete.
ECHO.
ECHO. *******************************************************
ECHO. *******************************************************



::Variables
SET JAVAINSTALL1="C:\Radia\Custom\20120200\x86\jre1.7.0_03.msi"
SET JAVAINSTALL2="C:\Radia\Custom\20120200\x64\jre1.7.0_03.msi"
SET JAVAREGISTRY="HKLM\SOFTWARE\JavaSoft\Java Plug-in\1.7.0_03"



::Checks to see if Java 6 X86 is installed: If yes, uninstall Java 6. If no, install Java 7
IF EXIST "C:\Program Files (x86)\Java\jre6" (
GOTO REMOVEJRE6X86
) ELSE (
GOTO JAVAx64CHECK
)



::Uninstall Java 6 x86 All Versions
:REMOVEJRE6X86
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216014FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216014FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216015FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216015FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216016FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216016FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216017FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216017FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216018FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216018FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216019FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216019FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216020FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216020FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216021FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216021FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216022FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216022FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216023FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216023FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216024FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216024FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216026FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216026FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216029FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216029FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216030FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216030FF}
Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F83216031FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F83216031FF}



::Checks to see if Java 6 X64 is installed: If yes, uninstall Java 6. If no, install Java 7
:JAVAx64CHECK
IF EXIST "C:\Program Files\Java\jre6" (
GOTO REMOVEJRE6X64
) ELSE (
GOTO INSTALLJAVA7X86
)



::Uninstall Java 6 x64 All Versions
:REMOVEJRE6X64
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416021FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416021FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416022FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416022FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416023FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416023FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416024FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416024FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416026FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416026FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416029FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416029FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416030FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416030FF}
Reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v {26A24AE4-039D-4CA4-87B4-2F86416031FF} 1>nul 2>nul
if errorlevel=0 start /wait MsiExec.exe /qn /x {26A24AE4-039D-4CA4-87B4-2F86416031FF}



GOTO INSTALLJAVA7X86



::Install Java 7 Update 3 X86
:INSTALLJAVA7X86
START /WAIT msiexec /qn /i %JAVAINSTALL1%
GOTO INSTALLJAVA7X64
)



::Install Java 7 Update 3 X64
:INSTALLJAVA7X64
START /WAIT msiexec /qn /i %JAVAINSTALL2%
GOTO EXIT
)



::Terminate Installation
:EXIT
EXIT


I have been told that I should create a "For loop" to have the script do what I want it to do but I do not have any experience in using these. Any one have any suggestions on how to clean up the batch listed above

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to uninstall any version of a software

#2 Post by foxidrive » 22 Mar 2012 11:14

Try this style of code. (Untested)

I also altered the errorlevel test.

Code: Select all

::Uninstall Java 6 x86 All Versions
:REMOVEJRE6X86
for %%a in (
{26A24AE4-039D-4CA4-87B4-2F83216014FF}
{26A24AE4-039D-4CA4-87B4-2F83216015FF}
{26A24AE4-039D-4CA4-87B4-2F83216016FF}
{26A24AE4-039D-4CA4-87B4-2F83216017FF}
{26A24AE4-039D-4CA4-87B4-2F83216018FF}
{26A24AE4-039D-4CA4-87B4-2F83216019FF}
{26A24AE4-039D-4CA4-87B4-2F83216020FF}
{26A24AE4-039D-4CA4-87B4-2F83216021FF}
{26A24AE4-039D-4CA4-87B4-2F83216022FF}
{26A24AE4-039D-4CA4-87B4-2F83216023FF}
{26A24AE4-039D-4CA4-87B4-2F83216024FF}
{26A24AE4-039D-4CA4-87B4-2F83216026FF}
{26A24AE4-039D-4CA4-87B4-2F83216029FF}
{26A24AE4-039D-4CA4-87B4-2F83216030FF}
{26A24AE4-039D-4CA4-87B4-2F83216031FF}
) do (

Reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v %%a 1>nul 2>nul
if errorlevel EQU 0 start /wait MsiExec.exe /qn /x %%a
)

JrSysEngineer
Posts: 5
Joined: 22 Mar 2012 10:34

Re: batch file to uninstall any version of a software

#3 Post by JrSysEngineer » 22 Mar 2012 11:40

That could work but I would rather not have to list all keys from 6 Update 14 through 7 Update 3 for both x86 and x64 platforms.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to uninstall any version of a software

#4 Post by foxidrive » 22 Mar 2012 12:19

What? What do you want to do?

I didn't check all the key variations, that ain't my job. :)

Is the bottom set of keys a subset of the top set of keys?

JrSysEngineer
Posts: 5
Joined: 22 Mar 2012 10:34

Re: batch file to uninstall any version of a software

#5 Post by JrSysEngineer » 23 Mar 2012 07:48

The top set of keys is all of the x86 versions of Java from 6 update 14 to 6 Update 31. The bottom set of keys is all of the x64 versions of Java from 6 Update 21 through 6 Update 31. What I want to do is not have to list all of the keys since only a few characters change between versions and platforms.


Essentially I would like to search the registry for a key and if it is found, run the uninstall registry value, then loop through the script again until the key is no longer found; if the key is not found skip directly to the install section. I want to use a key with wildcards in it since, like I said before, only a couple characters change from one version of Java to another.

Example:

{26A24AE4-039D-4CA4-87B4-2F8??1????FF}

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to uninstall any version of a software

#6 Post by foxidrive » 23 Mar 2012 08:50

JrSysEngineer wrote:The top set of keys is all of the x86 versions of Java from 6 update 14 to 6 Update 31. The bottom set of keys is all of the x64 versions of Java from 6 Update 21 through 6 Update 31. What I want to do is not have to list all of the keys since only a few characters change between versions and platforms.


You'll find it is more readable with the full keys as in the snippet I provided.

If you reduce the characters to the last 8 or so then you can put the leading set of characters directly into the two lines that query the registry and run the uninstaller - but it's not good practice as someone else looking at the code will find it less intuitive and you will also have to look twice when you want to update the code in 12 months or more.

As for the Java uninstaller - how will it handle a situation where the original update files have been removed?

JrSysEngineer
Posts: 5
Joined: 22 Mar 2012 10:34

Re: batch file to uninstall any version of a software

#7 Post by JrSysEngineer » 23 Mar 2012 09:25

Ok, so I took your advise and cleaned up the batch like in your snippet. It is definitely much more readable, thank you. I still would like to clean it up more so that I do not have to add a key to the batch file every time a new update to Java comes out, if this is even possible.

As to your question, I have thoroughly tested my original batch in XP, 7, Server 2K8, Server 2K3 x64 and Server 2K3 x86. I have not run into any issues if the original update files are not there. For example if a new computer is added to the network and it is loaded with Java 6 Update 31 while the rest of the computers on the network have been upgraded progressively from Update 14 through 31, the batch file will still uninstall Java 6 Update 31 with no issues, and then install Java 7 Update 3.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to uninstall any version of a software

#8 Post by foxidrive » 23 Mar 2012 09:46

Post the code you have now if you don't mind.


I recently had Java v6 29 loaded in XP, I think it was, and the later release wouldn't uninstall it because the java .msi file from V6 19 was missing.

It took a trawl through the registry deleting all kinds of things before I could update Java. I have another XP machine with the same issue - I'd like to test your solution.
FWIW I have some VBS solutions to remove Java that I found on the net - they were for previous releases though.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: batch file to uninstall any version of a software

#9 Post by Squashman » 23 Mar 2012 09:49

Foxidrive, I think this is what he is trying to do.

Code: Select all

For /F "Tokens=*" %%I In ('Reg Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall^|Findstr /I /C:"{26A24AE4-039D-4CA4-87B4-"')  Do (
  For /F "Tokens=4*" %%J In ('Reg Query "%%I" /V UninstallString') Do Set uninstall=%%K
)

JrSysEngineer
Posts: 5
Joined: 22 Mar 2012 10:34

Re: batch file to uninstall any version of a software

#10 Post by JrSysEngineer » 23 Mar 2012 10:04

I tried to do the error level like your snippet but it returned an error so I jet left that the same way I had it originally.

Code: Select all

ECHO OFF
CLS


ECHO. *******************************************************
ECHO. *******************************************************
ECHO.
ECHO.              DO NOT CLOSE THIS WINDOW
ECHO.
ECHO. Installing Java 7 Update 3
ECHO.
ECHO. Do not close this window. This window will close
ECHO. automatically when the installation is complete.
ECHO.
ECHO. *******************************************************
ECHO. *******************************************************



::Variables
SET JAVAINSTALL1="C:\Radia\Custom\20120200\x86\jre1.7.0_03.msi"
SET JAVAINSTALL2="C:\Radia\Custom\20120200\x64\jre1.7.0_03.msi"
SET JAVAREGISTRY="HKLM\SOFTWARE\JavaSoft\Java Plug-in\1.7.0_03"



::Checks to see if Java 6 X86 is installed: If yes, uninstall Java 6. If no, install Java 7
IF EXIST "C:\Program Files (x86)\Java\jre6" (
GOTO REMOVEJRE6X86
) ELSE (
GOTO JAVAX64CHECK
)



::Uninstall Java 6 x86 All Versions
:REMOVEJRE6X86
For %%a in (
{26A24AE4-039D-4CA4-87B4-2F83216014FF}
{26A24AE4-039D-4CA4-87B4-2F83216015FF}
{26A24AE4-039D-4CA4-87B4-2F83216016FF}
{26A24AE4-039D-4CA4-87B4-2F83216017FF}
{26A24AE4-039D-4CA4-87B4-2F83216018FF}
{26A24AE4-039D-4CA4-87B4-2F83216019FF}
{26A24AE4-039D-4CA4-87B4-2F83216020FF}
{26A24AE4-039D-4CA4-87B4-2F83216021FF}
{26A24AE4-039D-4CA4-87B4-2F83216022FF}
{26A24AE4-039D-4CA4-87B4-2F83216023FF}
{26A24AE4-039D-4CA4-87B4-2F83216024FF}
{26A24AE4-039D-4CA4-87B4-2F83216026FF}
{26A24AE4-039D-4CA4-87B4-2F83216029FF}
{26A24AE4-039D-4CA4-87B4-2F83216030FF}
{26A24AE4-039D-4CA4-87B4-2F83216031FF}
) DO (
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /v %%a 1>nul 2>nul
IF errorlevel=0 start /wait MsiExec.exe /qn /x %%a



::Checks to see if Java 6 x64 is installed: If yes, uninstall Java 6. If no, install Java 7
:JAVAX64CHECK
IF EXIST "C:\Program Files\Java\jre6" (
GOTO REMOVEJRE6X64
) ELSE (
GOTO INSTALLJAVA7X86
)



::Uninstall Java 6 x64 All Versions
:REMOVEJRE6X64
FOR %%b in (
{26A24AE4-039D-4CA4-87B4-2F86416021FF}
{26A24AE4-039D-4CA4-87B4-2F86416022FF}
{26A24AE4-039D-4CA4-87B4-2F86416023FF}
{26A24AE4-039D-4CA4-87B4-2F86416024FF}
{26A24AE4-039D-4CA4-87B4-2F86416026FF}
{26A24AE4-039D-4CA4-87B4-2F86416029FF}
{26A24AE4-039D-4CA4-87B4-2F86416030FF}
{26A24AE4-039D-4CA4-87B4-2F86416031FF}
) DO (
REG QUERY HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /v %%b 1>nul 2>nul
IF errorlevel=0 start /wait MsiExec.exe /qn /x %%b



GOTO INSTALLJAVA7X86



::Install Java 7 Update 3 X86
:INSTALLJAVA7X86
START /WAIT msiexec /qn /i %JAVAINSTALL1%
GOTO INSTALLJAVA7X64
)



::Install Java 7 Update 3 X64
:INSTALLJAVA7X64
START /WAIT msiexec /qn /i %JAVAINSTALL2%
GOTO EXIT
)



::Terminate Installation
:EXIT
EXIT

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: batch file to uninstall any version of a software

#11 Post by Squashman » 23 Mar 2012 10:13

Little revision to the code above as far as getting the uninstall string.

Code: Select all

@echo off
For /F "Tokens=*" %%I In ('Reg Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall^|Findstr /I /C:"{26A24AE4-039D-4CA4-87B4-"')  Do (
   For /F "skip=2 Tokens=2*" %%J In ('Reg Query "%%I" /V UninstallString') Do set uninstall=%%K
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to uninstall any version of a software

#12 Post by foxidrive » 23 Mar 2012 10:18

There can be a large number of those keys, Squashman.

Maybe iterating and just running the removal with all the keys matching {26A24AE4-039D-4CA4-87B4- would work, but I don't know how CLSID allocation works and if that's appropriate.


As for the errorlevel - I should have used this:
if %errorlevel% EQU 0

if errorlevel=0 will *always* be true



I note that this assumes a 64 bit machine too.

Post Reply