Skip to content

Instantly share code, notes, and snippets.

@adbertram
Created December 22, 2020 16:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adbertram/5cf6a7aa20ebe87e88b77c2a89431cf9 to your computer and use it in GitHub Desktop.
Save adbertram/5cf6a7aa20ebe87e88b77c2a89431cf9 to your computer and use it in GitHub Desktop.
$RegUninstallPaths = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)
$VersionsToKeep = @('Java 8 Update 261')
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
$UninstallSearchFilter = {($_.GetValue('DisplayName') -like '*Java*') -and (($_.GetValue('Publisher') -eq 'Oracle Corporation')) -and ($VersionsToKeep -notcontains $_.GetValue('DisplayName'))}
# 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
}
}
}
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
}
@nolme
Copy link

nolme commented Mar 2, 2021

on line 22, you should expand the environment variable %WINDIR% instead of using hardcoded path c:\Windows.
It will works in most cases but not all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment