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
  • 312,089
  • 56
  • 508
  • 622
jin xinbo
  • 33
  • 3
  • 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
  • 135,435
  • 9
  • 130
  • 184