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
dbsooner09
Engaged Sweeper III
I know this is a bit old but is this still the best way to remove all the old Java versions from systems? Thanks.
looktall
Engaged Sweeper III
Daniel B wrote:
I know this is a bit old but is this still the best way to remove all the old Java versions from systems? Thanks.


I don't know about best way, but i found my way to be pretty successful.

$javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
Get-ItemProperty |
Where-Object {$_.URLInfoAbout -match "java" } |
Select-Object -Property DisplayName, UninstallString

ForEach ($ver in $javaVer) {

If ($ver.UninstallString) {

$uninst = $ver.UninstallString
& cmd /c $uninst /quiet /norestart
}

}

PeterJG
Champion Sweeper II
now if you are using the .exe installer you can add switch that removed all old java versions for you automatically after install

I do it by installing first x86 and than x64 version with the (remove old java switch) and takes out all obsolete versions.


example "jre-8u131-windows-x64.exe" /s AUTO_UPDATE=0 EULA=0 NOSTARTMENU=1 SPONSORS=0 WEB_ANALYTICS=0 WEB_JAVA=1 WEB_JAVA_SECURITY_LEVEL=H REMOVEOUTOFDATEJRES=1"
EleaCarm
Engaged Sweeper II
I had problems with PowerShell 1 and Windows 10 for 2 reasons:

1. The execution security of powershell is restricted "Get-ExecutionPolicy" and the script does not correctly convert the string to the question.

Fixed by executing "Set-ExecutionPolicy -ExecutionPolicy Unrestricted"


2. The question in the script:

If (($ app.Version -ne $ newest64bitVersion) -and ($ newest64bitVersion -ne $ null)) {

I do not operate correctly because $ newest64bitVersion returns a collection of number and not a string. The line I operate correctly is:

If (("$ app.Version" -ne "$ newest64bitVersion") -and ($ newest64bitVersion -ne $ null)) {


3. As an alternative method, VBScript is executed, which is attached.
Gdsmcksysadmin
Engaged Sweeper
do you have a way to do it in mass? I am looking to run the script on 20-30 pc's
norbertogomes
Engaged Sweeper
I deployed the package in a single Windows 7 desktop client and I enabled it to run powershell script but still not working.
All java versions are installed yeat!

See if the script attached is correctly !
PeterJG
Champion Sweeper II
as long as the naming string hasn't changed it should work
Ruben1
Engaged Sweeper III
Still works fine here on Java 8 update 71.
norbertogomes
Engaged Sweeper
I have the lasted JavaVersion: 8u71 and I would like to know if these scripts are compatible to remove older versions.
When I run the script, the machine restarts twice but all previous version still installed !

See the log below:

Result: Package timeout reached. Task Error: The task is currently running. Timeout: (300Sec). Credential: ("DOMAIN\user). ShareCredential: (server\user).
Machine has:
Java 8 update 51
Java 8 update 65
Java 8 update 66
Java 8 update 71 (lasted version)

tks !