---
title: "Active Directory Health Check: PowerShell Scripting"
description: "Monitor the health of your Active Directory with our comprehensive PowerShell script. A must-have for IT admins."
canonical: "https://adamtheautomator.com/removing-old-java/"
---

# Active Directory Health Check: PowerShell Scripting

> Monitor the health of your Active Directory with our comprehensive PowerShell script. A must-have for IT admins.

Source: https://adamtheautomator.com/removing-old-java/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Active Directory Health Check: PowerShell Scripting](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea597.jpg)

# Active Directory Health Check: PowerShell Scripting

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)30 June 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Java](/tag/java/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Creating the Script](#h-creating-the-script)
*   [Define the Registry Paths](#h-define-the-registry-paths)
*   [Define Versions to Keep](#h-define-versions-to-keep)
*   [Find and Stop all Running Processes](#h-find-and-stop-all-running-processes)
*   [Search for Java in the Registry and Force an Uninstall](#h-search-for-java-in-the-registry-and-force-an-uninstall)
*   [Remove any Old Java Remnants](#h-remove-any-old-java-remnants)
*   [Wrap up](#h-wrap-up)

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 for removing old Java versions.

In this tutorial, you’re going to learn how to create a Java removal tool for removing old Java in PowerShell.

Let’s get started!

## Creating the Script

First, open up your favorite code editor like Visual Studio Code and save the file as _Remove-Java.ps1_. This PowerShell script will contain everything you need to clean up previous Java versions.

## Define the Registry Paths

Because Java instances can hide in two places in the registry, first define each of the parent registry keys. The below code snippet provides both the 32 and 64-bit registry paths.

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

## Define Versions to Keep

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

You can find the exact text to use by looking at Programs and Features in the Control Panel.

```powershell
$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 Java can embed itself within Internet Explorer and can’t be removed, go ahead and also stop all Internet Explorer processes too.

```powershell
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.

```powershell
$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.

```powershell
# 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
    
        }
    }
}
```

## Remove any Old Java Remnants

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

```powershell
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](https://twitter.com/s4mry4n) for helping me fix some bugs with this code._

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fremoving-old-java%2F&text=Active%20Directory%20Health%20Check%3A%20PowerShell%20Scripting)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fremoving-old-java%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fremoving-old-java%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
