How to Remove Java with PowerShell

Adam Bertram

Read more posts by this author.

If left unattended, Java runtime environments 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 of Java! It’s time to take back control and build a PowerShell script to remove Java!

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.

In this tutorial, you’re going to learn how to create a Java removal tool in PowerShell that you can run on all of your endpoints.

Let’s get started by first creating a PowerShell script called Remove-Java.ps1 in your favorite editor.

Related: All ATA posts on Visual Studio Code

Once you have a script open and ready, let’s build!

Defining the Registry Paths

First, you’ll need to define each of the registry keys where the Java runtime environment registers. The below code snippet provides both the 32 and 64-bit registry paths.

$RegUninstallPaths = @(
    'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
    'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)

Defining Versions to Keep

Because you probably want to keep some versions of Java out there, create an array with each of the versions you’d like to keep.

$VersionsToKeep = @('Java 8 Update 261')

Find and Stop all Running Processes

Next, find all of the running Java processes. The code snippet below enumerates all of the running processes with WMI that are running with a file in a path matching *Program Files\Java*.

Since can embed itself within Internet Explorer and can’t be removed, go ahead and also stop all Internet Explorer processes too.

Get-CimInstance -ClassName 'Win32_Process' | Where-Object {$_.ExecutablePath -like '*Program Files\Java*'} | 
    Select-Object @{n='Name';e={$_.Name.Split('.')[0]}} | Stop-Process -Force
 
Get-process -Name *iexplore* | Stop-Process -Force -ErrorAction SilentlyContinue

Search for Java in the Registry and Force an Uninstall

Next, build a filter that you’ll use to enumerate all of the values under the registry keys defined above. This filter finds all of the Java instances and excludes all versions you’d like to keep.

$UninstallSearchFilter = {($_.GetValue('DisplayName') -like '*Java*') -and (($_.GetValue('Publisher') -eq 'Oracle Corporation')) -and ($VersionsToKeep -notcontains $_.GetValue('DisplayName'))}

Now search through the registry to find all Java instances and, if found, run msiexec.exe /X to remove each instance.

# Uninstall unwanted Java versions and clean up program files
 
foreach ($Path in $RegUninstallPaths) {
    if (Test-Path $Path) {
        Get-ChildItem $Path | Where-Object $UninstallSearchFilter | 
       foreach { 
           
        Start-Process 'C:\Windows\System32\msiexec.exe' "/X$($_.PSChildName) /qn" -Wait
    
        }
    }
}

Cleanup any Java Remnants

Clean up any remnants that the Java uninstaller doesn’t get rid of on its own.

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

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

Wrap up

And that’s it! Run this PowerShell script on each computer you’d like to cleanup Java on to make this process as seamless as possible.

Feel free to download the entire script at once below.

Thanks to Sam Turner for helping me fix some bugs with this code.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!