Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Check or Update Java Version in Windows with PowerShell?

May 10, 2023 PowerShellWindows 10Windows Server 2016

How to Check or Update Java Version in Windows with PowerShell?

Java Runtime Environment (JRE) is widely used on user computers to run different enterprise Java apps . However, some apps require the specific Java version and may work incorrectly in other versions. In this article we will discuss how to check Java versions installed on computers in your network, and how to uninstall or update JRE using PowerShell.

Contents:
  • How to Check Java Version in Windows?
  • Check Java Version using PowerShell
  • PowerShell – Check Java Version on Remote Computers
  • PowerShell Script to Uninstall All Java Versions
  • How to Download and Install Java JRE Using PowerShell?

How to Check Java Version in Windows?

You can get the version number of Java installed on your computer if you enter java in Windows 10 search box and run Java applet.

windows 10 call java desktop app

In About Java window, the current JRE version is specified. In my case, it is Java Version 8 Update 261 (build 1.8.0_261-b12). Note the value of the JRE build. All Java versions have 1 in the beginning followed by the number of major JRE version (it is 8 in my case) and the update number.

java oracle applet - version

You can also check the current Java version in Windows Program and Features (Win+R -> appwiz.cpl).

installed java in the list of programs on windows 10

You can display the current Java version in the command prompt. Run cmd.exe and run the command:

java -version

java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode, sharing)

java -version cmd

Check Java Version using PowerShell

You can check Java version installed on your computer using PowerShell. You can just check the version of the executable file java.exe (the path to it is set in the environment variables when JRE SE is installed on your computer). Display the java file version:

Get-Command Java | Select-Object Version

Get-Command Java

You can view detailed information about Java version, update and release number:

Get-Command java | Select-Object -ExpandProperty Version

Major Minor Build Revision
----- ----- ----- --------
8 0 2610 12

If you want to get a string value of your Java version to be used in scripts, use the command:

(Get-Command java | Select-Object -ExpandProperty Version).tostring()

powershell check java version, build and revision

You can also find out your Java version through the WMI class Win32_Product (contains the list of installed programs in Windows):

Get-WmiObject -Class Win32_Product -Filter "Name like '%Java%'"

check installed java via Get-WmiObject -WMI Class Win32_Product

IdentifyingNumber : {26A24AE4-039D-4CA4-87B4-2F32180261F0}
Name : Java 8 Update 261
Vendor : Oracle Corporation
Version : 8.0.2610.12
Caption : Java 8 Update 261

IdentifyingNumber : {4A03706F-666A-4037-7777-5F2748764D10}
Name : Java Auto Updater
Vendor : Oracle Corporation
Version : 2.8.261.12
Caption : Java Auto Updater

The IDs may be used later to correctly uninstall JRE.

If you want to display only Java version without Java Auto Updater, use the following command:

Get-WmiObject -Class Win32_Product -Filter "Name like '%Java%' and not Name like '%Java Auto Updater%'" | Select -Expand Version

PowerShell – Check Java Version on Remote Computers

If you want to get Java versions used on all computers or servers in your domain, you can use the following PowerShell script. The script can get the information from all servers remotely according to the list you enter manually or from a text file. You can also get the list of servers or computers in AD using the Get-ADComputer cmdlet from the RSAT-AD-PowerShell module.
# PowerShell script to check Java SE (JRE) version on remote computers
# To check Java version on the computers in the list
# $computers = @('mun-sql01,mun-fs01,mun-sql02')
# Check Java version against the list of servers in a text file
#$computers=Get-content C:\PS\ServerList.txt
# To get Java version on all Windows Servers in AD domain
$computers = ((get-adcomputer -Filter { enabled -eq “true” -and OperatingSystem -Like ‘*Windows Server*’ }).name).tostring()
Get-WmiObject  -Class Win32_Product -ComputerName $computers -Filter “Name like ‘%Java%’ and not Name like ‘%Java Auto Updater%'” | Select __Server, Version

get java version from multiple remote computers

As a result, you will have a table with the list of computers/servers and Java versions installed on them.

In my case, when checking the Java version on domain computers, I found 24 different JRE versions!!!

PowerShell Script to Uninstall All Java Versions

Why may you need to uninstall previous Java versions in Windows?

  • Before you install a new Java version, it is better to uninstall all previous versions. Like in other products, new features appear and critical exploits are constantly fixed in Java. If you have older versions of Java installed, your computer is subject to infection or exploitation of known or 0-day vulnerabilities. Java has a built-in autoupdate mechanism, but on different reasons administrators may disable it on domain computers.
  • You have no paid Java JRE subscription. In 2019, Oracle changed Java licensing policy. If you want to use previous Oracle JDK (Java SE) versions, you must get a paid subscription. It refers to all Java JRE releases after April, 16, 2019 (from Java 8 SE Update 211).
    Commercial Java SE versions have a long-time support (updates are released for 5 years since the release date). A free Java version is Open JDK (distributed under GPL), but you have to update it every six months. Another Open JDK disadvantage is that it doesn’t have a convenient installer for Windows. You must download and install Open JDK manually. There is a nice option to install Open JDK using the WinGet package manager: winget install --id=ojdkbuild.openjdk.11.jdk -e

You can use the following PowerShell script to remove all installed Java versions on a local computer:

$jre_installed = Get-WmiObject -Class Win32_Product -Filter "Name like '%Java%'"
$jre_installed.Uninstall()

However, the uninstall command above is based on WMI and works slowly.

Instead, you can get a list of installed Java versions from the registry and uninstall all of them by their product GUID generated when you install software via MSI.

#PowerShell script to uninstall all Java SE (JRE) versions on a computer
$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -like "*Java*" } | select UninstallString
$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -like "*Java*" } | select UninstallString
# Uninstall 64-bit Java versions
if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe", "" -Replace "/I", "" -Replace "/X", ""
$uninstall64 = $uninstall64.Trim()
Write "Uninstalling Java ..."
start-process "msiexec.exe" -arg "/X $uninstall64 /qb" -Wait
}
# Uninstall 32-bit Java versions
if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
Write "Uninstalling all Java SE versions..."
start-process "msiexec.exe" -arg "/X $uninstall32 /qb" -Wait
}

automatic uninstall old java version via powershell

How to Download and Install Java JRE Using PowerShell?

The following PowerShell script automatically downloads the latest version of Java installer from the official website and installs it on a computer(you can download both online and offline installer). The install command suppress reboot request and disables automatic Java updates.

To download an installer file from the website, the Invoke-WebRequest cmdlet is used. If your computer is behind a proxy, configure you PowerShell to access Internet through a proxy server.

# PowerShell script to automatically download and install the latest Java SE (JRE) version
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Download an online Java installer
$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | % { [regex]::matches($_, '(?:<a title="Download Java software for Windows Online" href=")(.*)(?:">)').Groups[1].Value }
# Download an offline Java installer
#$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | % { [regex]::matches($_, '(?:<a title="Download Java software for Windows Offline" href=")(.*)(?:">)').Groups[1].Value }
Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL
Start-Process .\jre8.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait
echo $?

The first line in the script was added to use TLS 1.2. Otherwise, I had the following error:

Invoke-WebRequest: The request was aborted: Could not create SSL/TLS secure channel.

The script will automatically download a Java installation file, save it on a disk as jre8.exe and install.

Unfortunately, this method of getting the JRE setup file from the Oracle website stopped working just a few weeks ago. Now this webpage is generated by some .js script. And I cannot get the link to the java installer. Perhaps Oracle will change this soon.

4 comments
2
Facebook Twitter Google + Pinterest
previous post
CentOS: How to Set Date, Time, TimeZone and NTP Synchronization
next post
How to Reduce Windows.edb Huge File Size?

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

4 comments

Shlomi October 13, 2020 - 2:02 pm

Excellent article. Thank you so much for sharing with us!

Reply
Claude Coetzee September 24, 2021 - 9:12 am

When uninstalling JRE, it helps to use Publisher in your filter, otherwise your results include apps like Notepad++ that use the Uninstall reg key to store app configs that include the term “Java”.

$uninstall32 = gci “HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall” | foreach { gp $_.PSPath } | ? { $_ -like “*Java*” -and $_.Publisher -like “*Oracle*” } | select UninstallString
$uninstall64 = gci “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall” | foreach { gp $_.PSPath } | ? { $_ -like “*Java*” -and $_.Publisher -like “*Oracle*” } | select UninstallString

Reply
Claude Coetzee September 26, 2021 - 6:45 pm

Updated:
And filter out blank entries where no UninstallString was found.

$uninstalllist = $null
$uninstall = gci “HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall” | foreach { gp $_.PSPath } | ? { $_ -like “*Java*” -and $_.Publisher -like “*Oracle*” } | select UninstallString
$uninstall += gci “HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall” | foreach { gp $_.PSPath } | ? { $_ -like “*Java*” -and $_.Publisher -like “*Oracle*” } | select UninstallString
$uninstalllist = $uninstall.Where({$_.UninstallString -ne $null})

# Uninstall all Java versions
if ($uninstalllist) {
foreach ($cmdline in $uninstalllist) {
$guid = ($cmdline.UninstallString -Replace “msiexec.exe”, “” -Replace “/I”, “” -Replace “/X”, “”).Trim()
start-process “msiexec.exe” -arg “/X $guid /qn” -Wait
}
}

Reply
seo expert freelance July 7, 2022 - 8:27 am

HI

Do you know how to update the Java runtime from the BIOS?

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
  • Managing Saved Passwords Using Windows Credential Manager
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top