1

I am trying to figure out using Get-WmiObject -Class Win32_Product -ComputerName $System -Filter "Name like 'Java%'" | Select -Expand Version'" to return the latest version of the JAVA of the query.

it returns

8.0.2610.12
8.0.2810.9
8.0.2910.10
2.8.261.12

expect to return

8.0.2910.10
mklement0
  • 382,024
  • 64
  • 607
  • 775
jin xinbo
  • 33
  • 1
  • 4
  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) (v6+), where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Jul 21 '21 at 15:02

1 Answers1

2

Use the Sort-Object cmdlet to sort the version strings and then grab the largest value:

$versions = Get-WmiObject -Class Win32_Product -ComputerName $System -Filter "Name like 'Java%'" | Select -Expand Version 
$versions | Sort { $_ -as [version] } -Descending | Select -First 1
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206