Created
December 22, 2020 16:30
-
-
Save adbertram/5cf6a7aa20ebe87e88b77c2a89431cf9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.