Mar
11
2013

Clean up Java versions – Updated!

REVISED 11/03/13!

I’m not alone in having a headache whenever there’s a new Java-version. The old one doesn’t uninstall, the new one might fail to install. It’s a mess more often than not.

After spending some time trying to find a good way to clean it all up in an enterprise environment, I decided that there wasn’t a good solution.

So, I decided to invent the wheel myself. The result is a powershell script which does the following:

  1. Crawls the uninstaller-part of the registry, and uninstalls everything made by Oracle, which has Java in the displayname
  2. If it can’t uninstall for some reason, it removes the entries.
  3. Deletes the Javasoft folder in registry
  4. Cleans up Java from the installer section og HKEY_CLASSES_ROOT
  5. Deletes the Java programfolders from Programfiles (x86 and x64)
  6. Installs the latest version from a location you specify.

Run this during startup, and you should be all cleaned up. I’m not going in to details on how you can set it up to run on startup; there are too many options.

Any feedback on this will be welcome, based on all my tests it works flawlessly. You’ll just need the msi installer for Java for it to install properly.

CHANGELOG:

  • 11/03/13 – This version also kills all iexplore and java-processes; and also fully cleans up and updates x64 installs

The script is attatched as a .txt file (rename to ps1); and as follows. Don’t plagiarize, or I will flog you.

FILE: JavaCleanup

##Initial version 01.02.2013; Kristoffer Birkenes

##Revised 11.03.2013

 

#Set path to current Java-version

$msiexecinstallargs = ‘/i “PATH TO X86 INSTALLER” /qn /norestart ADDLOCAL=ALL IEXPLORER=1 MOZILLA=1 SYSTRAY=0’

$msiexecinstallargs64 = ‘/i “PATH TO X64 INSTALLER” /qn /norestart ADDLOCAL=ALL IEXPLORER=1 MOZILLA=1 SYSTRAY=0’

 

#Searchpath for uninstaller-keys (32- and 64-bit)

$RegPath = “HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall”

$RegPath64 = “HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”

 

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ErrorAction SilentlyContinue

$ClassesRootPath = “HKCR:\Installer\Products”

 

get-process -Name *iexplore* | Stop-Process -Force -ErrorAction SilentlyContinue

get-process -Name *java* | Stop-Process -Force -ErrorAction SilentlyContinue

 

 

#Searching registry, filtering installers with “Publisher” set to “Oracle”, and “Displayname” to “*Java*”.

$Results = Get-ChildItem $RegPath -Recurse -ErrorAction SilentlyContinue | foreach {gp ($_.pspath)}

$Results | where {$_.publisher -like “*Oracle*” -and $_.displayname -like “*Java*”} | foreach {

$_.displayname

#Execute uninstall-command

[string]$uninstallargs = “/X “+$_.pschildname+” /qn”

start-process msiexec.exe -args $uninstallargs -wait

 

#If uninstall should fail and key still exists, remove the entry by force

if ((test-path $_.pspath) -like “true”) {

remove-item $_.pspath -force -recurse

}

}

 

####64-BIT CLEANUP#####

 

if ((Get-WmiObject -Class Win32_OperatingSystem -ea 0).OSArchitecture -eq “64-bit”) {

 

get-process -Name *iexplore* | Stop-Process -Force -ErrorAction SilentlyContinue

get-process -Name *java* | Stop-Process -Force -ErrorAction SilentlyContinue

 

#Searching registry, filtering installers with “Publisher” set to “Oracle”, and “Displayname” to “*Java*”.

$Results = Get-ChildItem $RegPath64 -Recurse -ErrorAction SilentlyContinue | foreach {gp ($_.pspath)}

$Results | where {$_.publisher -like “*Oracle*” -and $_.displayname -like “*Java*”} | foreach {

$_.displayname

#Execute uninstall-command

[string]$uninstallargs = “/X “+$_.pschildname+” /qn”

start-process msiexec.exe -args $uninstallargs -wait

 

#If uninstall should fail and key still exists, remove the entry by force

if ((test-path $_.pspath) -like “true”) {

remove-item $_.pspath -force -recurse

}

}

}

 

#Clean out Java 7 and Java 6 entries from Classes_Root

$CRResults = Get-ChildItem $ClassesRootPath -Recurse -ErrorAction SilentlyContinue | foreach {gp ($_.pspath)}

$CRResults | where {$_.ProductName -like “Java*7*” -or $_.ProductName -like “Java*6*”} | foreach {

remove-item $_.pspath -force -recurse

}

 

#Remove the entire JavaSoft-key

 

if ((test-path HKLM:\SOFTWARE\JavaSoft) -like “true”) {

remove-item -recurse -path HKLM:\SOFTWARE\JavaSoft -force -ErrorAction SilentlyContinue

}

 

#Remove local files 32- and 64-bit

remove-item -recurse -path $env:Programfiles\Java\ -force -ErrorAction SilentlyContinue

 

if ((Get-WmiObject -Class Win32_OperatingSystem -ea 0).OSArchitecture -eq “64-bit”) {

remove-item -recurse -path ${env:ProgramFiles(x86)}\Java -force -ErrorAction SilentlyContinue

}

 

#Install new version

Start-Process msiexec.exe -Arg $msiexecinstallargs -Wait

 

if ((Get-WmiObject -Class Win32_OperatingSystem -ea 0).OSArchitecture -eq “64-bit”) {

Start-Process msiexec.exe -Arg $msiexecinstallargs64 -Wait

}

3 Comments + Add Comment

Leave a comment

*