-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListJavaVersions.ps1
More file actions
55 lines (48 loc) · 1.79 KB
/
ListJavaVersions.ps1
File metadata and controls
55 lines (48 loc) · 1.79 KB
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
# PowerShell Script to List All Installed Java Versions, Including Registry Entries and User Locations
function Get-JavaVersions {
# Paths to check for Java installations
$javaPaths = @(
"C:\Program Files\Java\*",
"C:\Program Files (x86)\Java\*",
"$env:LOCALAPPDATA\Programs\Java\*"
)
# Gathering Java installations from file system
$javaVersions = @()
foreach ($path in $javaPaths) {
if (Test-Path $path) {
Get-ChildItem -Path $path -Directory | ForEach-Object {
$version = $_.Name
$javaVersions += "Java Version: $version - Path: $($_.FullName)"
}
}
}
# Checking the registry for Java installations
try {
$javaRegPaths = @(
"HKLM:\SOFTWARE\JavaSoft\Java Runtime Environment",
"HKLM:\SOFTWARE\WOW6432Node\JavaSoft\Java Runtime Environment",
"HKCU:\SOFTWARE\JavaSoft\Java Runtime Environment",
"HKCU:\SOFTWARE\WOW6432Node\JavaSoft\Java Runtime Environment"
)
foreach ($regPath in $javaRegPaths) {
if (Test-Path $regPath) {
Get-ChildItem -Path $regPath | ForEach-Object {
$version = $_.GetValue("CurrentVersion")
$javaHome = $_.OpenSubKey($version).GetValue("JavaHome")
$javaVersions += "Java Version: $version - Path: $javaHome (from Registry)"
}
}
}
} catch {
Write-Warning "An error occurred while reading from the registry."
}
# Returning the found Java versions
if ($javaVersions.Count -eq 0) {
Write-Host "No Java installations found."
} else {
Write-Host "Found Java Installations:"
$javaVersions
}
}
# Execute the function
Get-JavaVersions