Search code examples
powershellsortingversion

How to return the latest version of java using Get-WmiObject


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

Solution

  • 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