→ 🚀What's New? Join Us for the Fall Product Launch! Register Now !

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Kinetic
Engaged Sweeper II

This package checks for both 32-bit and 64-bit installed versions of Java, and silently uninstalls any old versions leaving behind only the newest.

Keeping old versions of Java on your system presents a serious security risk.
Uninstalling older versions of Java from your system ensures that Java applications will run with the latest security and performance improvements on your system.

Either download the attached script, or copy the code below and save it as Remove_old_java_versions.ps1 at the {PackageShare}\Scripts folder

Notes:
- As this is just a simple Powershell script, it can also be run on its own.
- The script is a little slow as enumerating the WMI class Win32_Product takes a long time.

Update: Version 1.1 @ 2015-05-20
- Now also detects and uninstalls old Java non-update base versions (i.e. Java versions without Update #)
- Now also removes Java 6 and below, plus added ability to manually change this behaviour.
- Added uninstall default behaviour to never reboot (now uses msiexec.exe for uninstall)
Update: Version 1.2 @ 2015-07-28
- Bug fixes: null array and op_addition errors.

#This script is used to remove any old Java versions, and leave only the newest.
#Original author: mmcpherson
#Version 1.0 - created 2015-04-24
#Version 1.1 - updated 2015-05-20
# - Now also detects and removes old Java non-update base versions (i.e. Java versions without Update #)
# - Now also removes Java 6 and below, plus added ability to manually change this behaviour.
# - Added uninstall default behaviour to never reboot (now uses msiexec.exe for uninstall)
#Version 1.2 - updated 2015-07-28
# - Bug fixes: null array and op_addition errors.
#
#
# IMPORTANT NOTE: If you would like Java versions 6 and below to remain, please edit the next line and replace $true with $false
$UninstallJava6andBelow = $true

#Declare version arrays
$32bitJava = @()
$64bitJava = @()
$32bitVersions = @()
$64bitVersions = @()

#Perform WMI query to find installed Java Updates
if ($UninstallJava6andBelow) {
$32bitJava += Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "(?i)Java(\(TM\))*\s\d+(\sUpdate\s\d+)*$"
}
#Also find Java version 5, but handled slightly different as CPU bit is only distinguishable by the GUID
$32bitJava += Get-WmiObject -Class Win32_Product | Where-Object {
($_.Name -match "(?i)J2SE\sRuntime\sEnvironment\s\d[.]\d(\sUpdate\s\d+)*$") -and ($_.IdentifyingNumber -match "^\{32")
}
} else {
$32bitJava += Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "(?i)Java((\(TM\) 7)|(\s\d+))(\sUpdate\s\d+)*$"
}
}

#Perform WMI query to find installed Java Updates (64-bit)
if ($UninstallJava6andBelow) {
$64bitJava += Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "(?i)Java(\(TM\))*\s\d+(\sUpdate\s\d+)*\s[(]64-bit[)]$"
}
#Also find Java version 5, but handled slightly different as CPU bit is only distinguishable by the GUID
$64bitJava += Get-WmiObject -Class Win32_Product | Where-Object {
($_.Name -match "(?i)J2SE\sRuntime\sEnvironment\s\d[.]\d(\sUpdate\s\d+)*$") -and ($_.IdentifyingNumber -match "^\{64")
}
} else {
$64bitJava += Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "(?i)Java((\(TM\) 7)|(\s\d+))(\sUpdate\s\d+)*\s[(]64-bit[)]$"
}
}

#Enumerate and populate array of versions
Foreach ($app in $32bitJava) {
if ($app -ne $null) { $32bitVersions += $app.Version }
}

#Enumerate and populate array of versions
Foreach ($app in $64bitJava) {
if ($app -ne $null) { $64bitVersions += $app.Version }
}

#Create an array that is sorted correctly by the actual Version (as a System.Version object) rather than by value.
$sorted32bitVersions = $32bitVersions | %{ New-Object System.Version ($_) } | sort
$sorted64bitVersions = $64bitVersions | %{ New-Object System.Version ($_) } | sort
#If a single result is returned, convert the result into a single value array so we don't run in to trouble calling .GetUpperBound later
if($sorted32bitVersions -isnot [system.array]) { $sorted32bitVersions = @($sorted32bitVersions)}
if($sorted64bitVersions -isnot [system.array]) { $sorted64bitVersions = @($sorted64bitVersions)}
#Grab the value of the newest version from the array, first converting
$newest32bitVersion = $sorted32bitVersions[$sorted32bitVersions.GetUpperBound(0)]
$newest64bitVersion = $sorted64bitVersions[$sorted64bitVersions.GetUpperBound(0)]

Foreach ($app in $32bitJava) {
if ($app -ne $null)
{
# Remove all versions of Java, where the version does not match the newest version.
if (($app.Version -ne $newest32bitVersion) -and ($newest32bitVersion -ne $null)) {
$appGUID = $app.Properties["IdentifyingNumber"].Value.ToString()
Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /norestart /x $($appGUID)" -Wait -Passthru
#write-host "Uninstalling 32-bit version: " $app
}
}
}

Foreach ($app in $64bitJava) {
if ($app -ne $null)
{
# Remove all versions of Java, where the version does not match the newest version.
if (($app.Version -ne $newest64bitVersion) -and ($newest64bitVersion -ne $null)) {
$appGUID = $app.Properties["IdentifyingNumber"].Value.ToString()
Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /norestart /x $($appGUID)" -Wait -Passthru
#write-host "Uninstalling 64-bit version: " $app
}
}
}
26 REPLIES 26
Ruben1
Engaged Sweeper III
@Daniel: I noticed this is normal behavior. I will always leave the latest version of the 32bit AND the latest version of the 64bit. In order to remove the older 32bit, you also need to install the new version of the 32bit.
DanielB
Engaged Sweeper
thx for the nice script 😉

I use it after installing the newest version.

But we also have the problem that it doesn't uninstall 32bit versions on 64bit machines.
For testings I installed an older 64bit version that got uninstalled with the script, but the 32bit version stayed.

Thanks,
Daniel
sarawitherow
Engaged Sweeper
I modified the script to have a value passed at the command line of the version of Java that will not be uninstalled.

Sometimes our users will update their Java past the company approved version that has been tested to be fully compatible with our Oracle business applications. So with this modification, it will remove all versions except for the version I feed the script - both older and newer.

Works great. Thanks!
🙂
Sara
sarawitherow
Engaged Sweeper
I think I know. It seems the logic of the script is to check the versions on the machine, see which one has the highest value, and remove any of the items below that value.

My method of updating machines is to first remove all the older versions, then install the new update. So in this scenario, the latest would have to first be installed.

I would love a version that I could feed it the "new version" to the script so that it does remove all the older ones first.

PeterJG
Champion Sweeper II
it should remove both.. 32 and 64 bit.. it does on our pc's .. 99% of them are win7 64bit and works without issues.. there might be something else going on on yours.
sarawitherow
Engaged Sweeper
Great script. Just have one issue to ask about.

On our 64-bit machines, we have both the 32-bit Java and the 64-bit Java installed as it helps with some of our business applications.

The script run on a 64-bit machine only uninstalls the 64-bit Java. Is there an easy modification to have it remove the 32-bit as well?

Thanks,
Sara
laurin1
Engaged Sweeper III
I need to also remove the old JDK versions. Any tips on how to modify this before I spend a bunch of time figuring it out on my own.
steefLCSO
Engaged Sweeper
Hi PeterG,

Sadly it did not work for me, even with the increase the package execution time.
The same warning appears Package timeout reached. . Timeout: (900Sec).
Might this be a permisson problem ?

Cheers
PeterJG
Champion Sweeper II
try increasing package execution time from 15min to 30min and see if still doesn't work. You will also get package execution timeout if the package started and never got reply from client to server that is done due to network connection loss.
steefLCSO
Engaged Sweeper
Hi Kinetec,

I think your script works fine.
I only get the following error message in my lansweeper deployment log: Result: Package timeout reached. . Timeout: (300Sec).
Do you have any idea how to fix this issue ?

Regards