If left unattended, Java can spread like wildfire in an enterprise. When I ran a report to find out what the Java landscape looked like in my environment, I found 62 different versions Java! This was from years of users clicking Yes to auto-update prompts, websites forcing Java and, however, else it got spread.

I'm still in the planning stages of this massive cleanup and upgrade effort. Here's the cleanup script I wrote with help from Kristoffer @ 3techies.com. Other than finding and uninstalling all versions of Java, I included an option to keep certain versions. We've got a need to keep a few old versions out there due to some old software that requires it. After it gets done doing the uninstalls, it then cleans some of the remnants from the registry that any version may have left.

This script works great. As a test, I installed dozens of different versions across a few different VMs and it uninstalled them all without fail!

$RegUninstallPaths = @(
$VersionsToKeep = @('Java 7 Update 5')

Get-WmiObject Win32_Process | Where {$_.ExecutablePath -like '*Program FilesJava*'} | 
    Select @{n='Name';e={$_.Name.Split('.')[0]}} | Stop-Process -Force

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

$UninstallSearchFilter = { ($_.GetValue('DisplayName') -like '*Java*') -and 
    (($_.GetValue('Publisher') -eq 'Oracle') -or
    ($_.GetValue('Publisher') -eq 'Sun Microsystems, Inc.')) -and
    ($VersionsToKeep -notcontains $_.GetValue('DisplayName'))} 

foreach ($Path in $RegUninstallPaths) {
    if (Test-Path $Path) {
        Get-ChildItem $Path | Where $UninstallSearchFilter | 
        Foreach { Start-Process 'C:\Windows\System32\msiexec.exe' "/x $($_.PSChildName) /qn" -Wait}

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
$ClassesRootPath = "HKCR:\InstallerProducts"
Get-ChildItem $ClassesRootPath | 
    Where { ($_.GetValue('ProductName') -like '*Java*7*') -or ($_.GetValue('ProductName') -like "Java*6*")} |
    Foreach {Remove-Item $_.PsPath -Force -Recurse}

$JavaSoftPath = 'HKLM:\SOFTWARE\JavaSoft'
if (Test-Path $JavaSoftPath) {
    Remove-Item $JavaSoftPath -Force -Recurse

Remove-Item $env:ProgramfilesJava -Force -Recurse

Find this script on my GitHub repo.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!