How to get the Java version in PowerShell

54,955

Solution 1

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.txt

del .\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()

Solution 2

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 Version

Version
-------
8.0.1710.11



PS > Get-Command java | Select-Object -ExpandProperty Version

Major  Minor  Build  Revision
-----  -----  -----  --------
8      0      1710   11



PS > (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.

Share:
54,955
Ivan
Author by

Ivan

Continuous Delivery, Containers, Clouds, Agile, DevOps Infrastructure as Code, Pipeline as Code TDD, KISS, DRY, YAGNI, GyShiDo Skydiving, Motorcycle Racing, Bodybuilding, Meditation https://www.linkedin.com/in/boykoivan/

Updated on July 24, 2020

Comments

  • Ivan
    Ivan almost 4 years

    I'm trying to get the Java version in PowerShell. The version string is printed to stderr, so I'm trying to redirect it to stdout and assign it to a string variable.

    I get the following strange error:

    PS P:\> & java -version 2>&1
    java.exe : java version "1.7.0_25"
    At line:1 char:2
    + & <<<<  java -version 2>&1
        + CategoryInfo          : NotSpecified: (java version "1.7.0_25":String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
    
    Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
    Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
    

    Call without redirection (2>&1) gives this:

    PS P:\> & java -version
    java version "1.7.0_25"
    Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
    Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
    

    I think that Java here is irrelevant, and the same would happen for any other program printing strings to stderr.

    The PowerShell version I use is 2.0.

    Questions:

    • How can I redirect stderr to a variable?
    • Or, alternatively, how can I check the installed Java version?

    Workaround

    I can run it like this:

    $output = & cmd /c "java -version 2>&1"
    

    But I hate running a cmd.exe where it shouldn't be necessary.

  • Ivan
    Ivan over 10 years
    Good approach! Didn't work for me just like this (something to do with the -Filter), but has worked as: Get-WmiObject -Class Win32_Product | Where-Object {$_.Name.StartsWith('Java')} | Select -Expand Version
  • Ivan
    Ivan over 10 years
    Thanks. Yes, I knew about -RedirectStandardError option of Start-Process, but hate creating a file just for this simple reason...
  • CB.
    CB. over 10 years
    @Ivan Added my last ;)
  • Ivan
    Ivan over 10 years
    Amazing! Couldn't think there are so many ways of getting Java version. Will probably use the one with Get-WmiObject. Though it's still not clear how to redirect stderr in PS... but many thanks C.B.!
  • CB.
    CB. over 10 years
    @Ivan Glad to help! I've added a method for redirect stderr, hope you like it
  • Bassie
    Bassie almost 6 years
    The Get-WmiObject approach just causes my script to hang, how annoying
  • Ross Presser
    Ross Presser over 4 years
    This works great, but be aware that the version will be reported as (e.g.) 8.0.910.14 by this method, whereas java -version reports 1.8.0_91
  • Paul Wetter
    Paul Wetter over 3 years
    Querying the Win32_Product for anything is not recommended. Will cause a system to basically touch every single app installed via msi on a computer.