Link to home
Start Free TrialLog in
Avatar of member 01
member 01

asked on

check java version using powershell

Hi,

I want to check java version in window server 2012 r2 . please let me know the process. Many scripts available in internet are not working though.
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Try java --version at command line
Avatar of member 01
member 01

ASKER

only powershell is required
Avatar of Shaun Vermaak
$key = 'HKLM\SOFTWARE\JavaSoft\Java Runtime Environment' (Get-ItemProperty -Path $key -Name CurrentVersion).CurrentVersion

Open in new window

This should work:

(Get-WmiObject Win32_Product | Where {$_.Name -match "Java"}).Version

Open in new window

~bp
I tried that but it only hangs on the prompt so I didn't post it
Scratch that, seems my original test finished. It just took a while
Shaun,

The code does not work in PowerGUI IDE:

+ $key = 'HKLM\SOFTWARE\JavaSoft\Java Runtime Environment' (Get-ItemPro ...
+                                                          ~
Unexpected token '(' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
shaun,

IT does not work


Bill ,
Its giving two outputs, one is correct one is incorrect
ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since several versions of Java can be installed at the same time (including the JDK), querying the "CurrentVersion" is not reliable enough.
This will retrieve all Java components, for both x86 and x64:
'', 'Wow6432Node\' |
	ForEach-Object {Get-ItemProperty -Path HKLM:\SOFTWARE\$($_)Microsoft\Windows\CurrentVersion\Uninstall\* |
	Where-Object {($_.DisplayName -like '*Java *') -and (-not $_.SystemComponent)} |
	Select-Object DisplayName, DisplayVersion, @{n='Architecture'; e={If ($_.PSParentPath -like '*Wow6432Node*') {'x86'} Else {'x64'}}}}

Open in new window


Bill,
never, ever, use Win32_Product. Want to know why it's so slow? Because it's not just a simple 'read' query - it actively changes the system, because the WMI provider will run a validation (and repair) for each and every msi package found on the system.

Event log message indicates that the Windows Installer reconfigured all installed applications
https://support.microsoft.com/en-us/kb/974524
Win32_product Class is not query optimized. Queries such as “select * from Win32_Product where (name like 'Sniffer%')” require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the “where” clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure.
@oBdA,

Duly noted, thanks.

(Note:  Reference link for additional info on the Win32_Product risks found here.)

Good solution approach.

~bp
Hi this command is working

Get-ItemProperty -Path "HKLM:\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment" -Name CurrentVersion



now
I need to check java version and check if 6.29 is installed or not. If any other version are installed than reinstall 6.29.
please suggest