How to get the Java version in PowerShell How to get the Java version in PowerShell powershell powershell

How to get the Java version in PowerShell


One way is using WMI:

$javaver =  Get-WmiObject -Class Win32_Product -Filter "Name like 'Java(TM)%'" | Select -Expand Version

Another one is redirect to a file with start-process:

start-process  java  -ArgumentList "-version" -NoNewWindow -RedirectStandardError .\javaver.txt$javaver = gc .\javaver.txtdel .\javaver.txt

And my last is:

dir "HKLM:\SOFTWARE\JavaSoft\Java Runtime Environment"  | select -expa pschildname -Last 1

Regarding how redirect stderr in this case you can do:

$out = &"java.exe" -version 2>&1$out[0].tostring()


I have been using Get-Command to get Java version on PowerShell 5.1.

Get-Command java | Select-Object Version

This returns an object. If you want a string instead, use:

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

The outputs look like this:

PS > Get-Command java | Select-Object VersionVersion-------8.0.1710.11PS > Get-Command java | Select-Object -ExpandProperty VersionMajor  Minor  Build  Revision-----  -----  -----  --------8      0      1710   11PS > (Get-Command java | Select-Object -ExpandProperty Version).tostring()8.0.1710.11

It worked quite well under PowerShell 5.1. I don't have a chance to test this on PowerShell 2.0.