-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_java
64 lines (50 loc) · 2.92 KB
/
remove_java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#meant to be deployed with something like pdq, intune, etc
$registryPath = "HKLM:\SOFTWARE\JavaTracking"
if (-not (Test-Path $registryPath)) {
Write-Output "Registry path does not exist. Exiting script."
exit
}
$uninstallRegistryKeys = @(
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall",
"HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)
# loops through registries to see anywhere where java may be installed
foreach ($key in $uninstallRegistryKeys) {
$subKeys = Get-ChildItem -Path $key -ErrorAction SilentlyContinue
foreach ($subKey in $subKeys) {
$displayName = (Get-ItemProperty -Path $subKey.PSPath -Name "DisplayName" -ErrorAction SilentlyContinue).DisplayName
$uninstallString = (Get-ItemProperty -Path $subKey.PSPath -Name "UninstallString" -ErrorAction SilentlyContinue).UninstallString
if ($displayName -and $displayName -like "Java*") {
Write-Output "Found Java installation: $displayName"
if ($uninstallString) {
Write-Output "Preparing to uninstall $displayName..."
# executes the uninstall string
if ($uninstallString -match "msiexec") {
$productCode = $uninstallString -replace ".*msiexec.*\s*\/x\s*", "" -replace "\s.*", ""
Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $productCode /quiet" -Wait -NoNewWindow
} else {
Start-Process -FilePath $uninstallString -Wait -NoNewWindow
}
# tells us if uninstall succesful.
if ($LASTEXITCODE -eq 0) {
Write-Output "Successfully uninstalled $displayName."
# remove prefetch files that are generated by java usage on the machine to avoid future scanning conflicts
$prefetchFiles = Get-ChildItem -Path "C:\Windows\Prefetch\" -Filter "JAVA*.pf" -ErrorAction SilentlyContinue
foreach ($file in $prefetchFiles) {
Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue
Write-Output "Removed Prefetch file: $($file.FullName)"
}
# creates our registry key that tells us java was uninstalled
$javaRemovedDate = Get-Date
Set-ItemProperty -Path $registryPath -Name "JavaRemovedDate" -Value $javaRemovedDate.ToString()
Write-Output "Registry updated with 'JavaRemovedDate': $javaRemovedDate"
} else {
Write-Output "Failed to uninstall $displayName. Please check manually."
}
} else {
Write-Output "No uninstall string found for $displayName."
}
}
}
}
Write-Output "Uninstallation process complete. Please verify in Control Panel or manually remove any remaining Java components if needed."