powershell less than / less and equal issue RRS feed

  • Question

  • 2.0.9 -lt 2.0.11 returns false

    2.0.9 -le 2.0.11 returns true 

    anyone who can enlighten me why ? 

    • Edited by Vagho Wednesday, August 3, 2016 9:10 AM
    Wednesday, August 3, 2016 9:09 AM


All replies

  • Hi Vagharsh,

    2.0.9 is no longer a valid  numeric value.

    PS C:\> $a=2
    PS C:\> $a.GetType()
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Int32                                    System.ValueType
    PS C:\> $b=2.0
    PS C:\> $b.GetType()
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Double                                   System.ValueType
    PS C:\> $c=2.0.9
    PS C:\> $c.GetType()
    You cannot call a method on a null-valued expression.
    At line:1 char:1
    + $c.GetType()
    + ~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    So, your assumption that it will return True in first case is incorrect. 

    Likewise, for the second statement, even this will return True.

    PS C:\> 2.0.12 -le 2.0.9

    For version comparisons, you have to split and compare each numeric value.

    Wednesday, August 3, 2016 9:45 AM
  • See this for an easy way to compare version numbers:


    • Proposed as answer by Chen VMVP Wednesday, August 3, 2016 1:56 PM
    • Marked as answer by Richard MuellerMVP Saturday, August 27, 2016 9:35 PM
    Wednesday, August 3, 2016 12:51 PM
  • "2.0.9" is not a number, but it might be a version.

    PowerShell can compare version number values with the [Version] class:

    PS C:\> ([Version] "2.0.9") -le ([Version] "2.0.11")

    Watch out, though as the [Version] class behaves incorrectly, in my opinion, when less significant values are missing from the version number:

    PS C:\> ([Version] "2.0") -eq ([Version] "2.0.0")

    (In my opinion .NET is simply wrong and should return True here, but that's just my opinion based on my reading of the API spec.)

    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 3, 2016 2:07 PM