MSEndpointMgr

Java Version Cleanup PowerShell Script

If you still have to maintain Java installations in your environment you will probably come up against having machines with multiple versions installed, unless you are running a clean up script for the old releases.

It is however sometimes a requirement to keep a specific release of Java for application compatibility, so what do you do in those scenarios where you want to clean up your old installs but the required version.

The below PowerShell script will do just that. It looks at your installed versions of Java, removes all previous versions and provides you with the option of specifying a version to keep during the process.

Script Download Link – https://gallery.technet.microsoft.com/scriptcenter/Java-Version-Cleaner-4a73f6be

<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.128
Created on: 13/11/2016 22:13
Created by: Maurice.Daly
Organization:
Filename: JavaVersionCleaner.ps1
===========================================================================
.DESCRIPTION
This script removes previous versions of Oracle Java.
The script supports the option to specify specific versions to keep
during the uninstall process.
.EXAMPLE
.\JavaVersionCleaner.ps1 -MinJavaVersion jre1.8.0_100
Using this will keep the current version of Java and also jre1.8.0_100

.\JavaVersionCleaner.ps1
This will remove all versions bar the current version of Java
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (

[parameter(Mandatory = $false, HelpMessage = “Specify Java Version to maintain”, Position = 1)]

[ValidateNotNullOrEmpty()] [string]$MinJavaVersion ) function JavaX64Uninstall ($MinJavaVersion) { Write-Debug “Running X64 Uninstalls” # Read registry for Java 64 bit versions $JavaX64Installs = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Recurse | Get-ItemProperty | Where-Object { ($_.displayName -like “*Java*Update*”) -and ($_.displayName -notlike “*Java*Auto*”) } | Select Displayname, UninstallString, InstallDate, InstatallLocation $JavaX64Count = $JavaX64Installs.Count foreach ($JavaX64Install in ($JavaX64Installs | Sort-Object InstallDate)) { if (($JavaX64Count) -gt 1) { if ($JavaX64Install.InstallLocation -notlike “*$($MinJavaVersion)*”) { $MSIString = $JavaX64Install.Uninstallstring | Split-Path -Leaf Write-Debug “Uninstalling $($JavaX64Install.Displayname)” Write-Debug “MSIString $($MSIString)” Start-Process MSIEXEC.EXE -ArgumentList (” /” + $MSIString + ” /QN”) } $JavaX64Count– } } } function JavaX86Uninstall ($MinJavaVersion) { Write-Debug “Running X86 Uninstalls” # Read registry for Java 32 bit versions $JavaX86Installs = Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall -Recurse | Get-ItemProperty | Where-Object { ($_.displayName -like “*Java*Update*”) -and ($_.displayName -notlike “*Java*Auto*”) } | Select Displayname, UninstallString, InstallDate, InstallLocation $JavaX86Count = $JavaX86Installs.Count foreach ($JavaX86Install in ($JavaX86Installs | Sort-Object InstallDate)) { if (($JavaX86Count) -gt 1) { if ($JavaX86Install.InstallLocation -notlike “*$($MinJavaVersion)*”) { $MSIString = $JavaX86Install.UninstallString | Split-Path -Leaf Write-Debug “Uninstalling $($JavaX86Install.Displayname)” Write-Debug “MSIString $($MSIString)” Start-Process MSIEXEC.EXE -ArgumentList (” /” + $MSIString + ” /QN”) } $JavaX86Count– } } } # Check for Windows 32 or 64 bit and process functions if ((Test-Path -Path “C:\Program Files (x86)”) -eq $true) { JavaX64Uninstall ($MinJavaVersion) JavaX86Uninstall ($MinJavaVersion) }else{ JavaX86Uninstall ($MinJavaVersion) } [

[sgmb id=1]

(9885)

Terence Beggs

An Irish man living in London, after completing a BSc in Computer Science in 2005 he started working in the IT Industry. Currently Senior Systems Officer at London Metropolitan University managing Azure and several thousand endpoints across several campuses in London. Technology focuses include SCCM, MDT, Azure, Office 365, Active Directory, Group Policy, Application Packaging, PowerShell, Virtualization and Automation.

2 comments

Sponsors