Uninstall Java the Easy Way.

Posted: August 8, 2013 in Automation, powershell

Much has been posted in regards to Java and how to uninstall it. A lot of scripts rely on knowing every product GUID in the registry and calling msiexec for each GUID to rid yourself of Java. After a bit of research I came up with the following approach using Powershell and WMI that avoids needing to know every GUID.

It uses a WMI query to find any product names that match specific strings. You’ll see in the code I look for ‘Java %’, with a space as there are lots of visual studio add ins that refer to “javascript”. I also look for ‘Java(TM)%’ as the JRE 6 runtime uses this specific string in it’s name. I also put into the query a filter to exclude Java Auto Updater which will be removed by the main program uninstaller and if called directly throws a 1603 return code.

I have also added a check to find any running Java processes. This is necessary as if you call the Uninstall() method, with Java processes running, the computer will reboot immediately on uninstall, without any warning.

The final part of the script will loop through the installed programs and call the Uninstall() method on each one. I plan to add a check to the script to verify that the program has an uninstall method, as some programs don’t have this method.

A word of warning that this has only been tested in my environment and I give no guarantee that this script will work correctly in your environment or won’t cause any unexpected results. You should test this script in your environment properly before use.

EDIT: I’ve updated the script to provide some more functionality and parameters in line with the Powershell Module version in a later post. I suggest you read the later post. The code is now hosted as a codeplex project to make any future changes easier to manage. Link is below.

https://poshuninstalljava.codeplex.com/

Advertisement
Comments
  1. Tom Aguero says:

    How did you run this on all you clients? Just via an SCCM package?

    • Yes, Just a simple package and program in SCCM. Use the powershell.exe command in the program to call the script and watch out for the powershell execution policy. Either use Group Policy to change the behaviour on the workstations, sign the script or use the -bypass switch.

  2. Jason says:

    You, sir, should have your own action figure. Thanks for the script!

  3. Torrente says:

    This is what I searched for damn long! Thank you so much dude!

  4. Carsten says:

    Here is my variant.

    [array]$javas=Get-WmiObject -query “select * from win32_Product where (Name like ‘Java %’ or Name like ‘Java(TM)%’ or Name like ‘J2SE%’) and (Name ‘Java Auto Updater’) and ((Vendor=’Sun Microsystems, Inc.’) or (Vendor=’Oracle’)) and (NOT Name like ‘%CompuGROUP%’) and (NOT Name like ‘%IBM%’) and (NOT Name like ‘%DB%’) and (NOT Name like ‘%Advanced Imaging%’) and (NOT Name like ‘%Media Framework%’) and (NOT Name like ‘%SDK%’) and (NOT Name like ‘%Development Kit%’) “

  5. JP says:

    How would you alter this to keep a specific version of Java on the machine? We want to keep 7.45 on our computers but remove everything else.

    • I’ve just finished writing a new version of the script that is now transformed into a full cmdlet with an optional argument that will let you specify a version to keep installed (e.g Uninstall-Java -KeepVersion “7.45”). I need to do some testing to confirm the functionality works and then I’ll release it. PS I’ve added Carsten’s additional exclusions into the query as well.

  6. […] had a couple of comments from my previous post on uninstalling Java with Powershell. They have been in regards to either enhancing the WMI query or leaving a particular Java version […]

  7. harribo48 says:

    hi there, your link to the powershell script doesn’t seem to work. any chance of a re-post? Anyone know why I can uninstall all versions of java on a PC install a new version which java.com/verify and javatester.org confirm only the new version is installed (even able to play java based games fine) but when trying to run server based applications needing java I’m prompted to install. The prompt only happens for existing users of a PC and if someone who’s never logged onto a PC before tries to access the same server based application it works every time so leads me to think either a file\folder or registry entry is left behind from the uninstall or something with the installer isn’t registering/over writing something. Any help would be appreciated.

  8. Link appears to be broken

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s