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)
}

PowerShell – Keeping data secure – Move all items from a users desktop to their My Documents

In my environment I have a constant battle with employees saving items on their desktops and expecting desktop support to be able to recover items when they are lost due to accidental deletion or hardware failure. Desktop remapping is an option but I like to allow users to store personal items on their machine (well at least its better than storing them on the network), hence the simple script below.

The script works by detecting environment variables for the users My Documents (obviously a remapped network location is recommended) and creating a subfolder within it called “Desktop Items”. It then starts a copy process before removing all items except shortcuts and the Personal folder from the users desktop.

If you want to be courteous to your users I also recommend using a GPO preference item to create a shortcut to their “Desktop Items” on their desktop. How you deploy the script is up to you, personally I use group policy preferences to create a shortcut to the powershell.exe in the users start up folder.

###############################################################################
#                                                                             #
# ****************** CREATED BY MAURICE DALY ON 16/01/2014 ****************** #
#                                                                             #
# Moves items from users desktop to a subfolder of their My Documents         #
#                                                                             #
# Version 1.0                                                                 #
# Version 1.1 Added functionality to copy leftover excluded files to MISC     #
#             Directory                                                       #
#                                                                             #
###############################################################################

$docspath = [environment]::getfolderpath("mydocuments") + "\Desktop Items"
$desktop = [environment]::getfolderpath("desktop")
$exclusions = @("*.iso", "*.lnk", "*.mp*", "*.exe", "*.msu", "*.url", "*.wav")
$mediafiles = @("*.iso", "*.mp*", "*.exe", "*.msu", "*.wav")

if (!(Test-Path $docspath))
{
new-item -Path $docspath -ItemType Directory
}

# List items to be copied / deleted
$filedirlist = Get-ChildItem -Path $desktop -Recurse -Exclude $exclusions | where FullName -NotLike *Personal* | where FullName -NotLike *Misc*

# Copy files and folders
$filedirlist |
Move-Item -Destination {
if ($_.PSIsContainer)
{
Join-Path $docspath $_.Parent.FullName.Substring($desktop.length)
}
else
{
Join-Path $docspath $_.FullName.Substring($desktop.length)
}
} -Force -Exclude $exclusions

# Set Miscellaneous folder location
$miscdocspath = [environment]::getfolderpath("desktop") + "\Misc"

# Collect list of legacy files to be moved
$legacyfilelist = Get-ChildItem -Path $desktop -Recurse -Include $mediafiles | where FullName -NotLike *Personal* | where FullName -NotLike *.lnk | where FullName -NotLike *Misc*

# Conditional statement to create a "Miscellaneous" directory for left over legacy files if they exist
if ($legacyfileslist -eq $null)
{
if (!(Test-Path $miscdocspath))
{
new-item -Path $miscdocspath -ItemType Directory
}
}

$legacyfilelist |
Copy-Item -Destination {
if ($_.PSIsContainer)
{
Join-Path $miscdocspath $_.Parent.FullName.Substring($desktop.length)
}
else
{
Join-Path $miscdocspath $_.FullName.Substring($desktop.length)
}
} -Force -ErrorAction SilentlyContinue