Win10 vs Win 8.1 os version

This topic contains 6 replies, has 4 voices, and was last updated by  Alexandru 1 year, 4 months ago.

  • Author
  • #56386


    Any idea why this is happening ?
    On a windows 10

    PS C:\> $os = Get-WmiObject win32_operatingsystem
    PS C:\> $o.version -gt 6.2

    While in Win 8.1

    PS C:\> $os = Get-WmiObject win32_operatingsystem
    PS C:\> $o.version -gt 6.2
  • #56389

    Don Jones

    See the documentation at

    Version is a string; you can't do numeric comparisons with it, unless you cast it as a [single] first. And even that will fail if the version is ever something like 6.2.2, since that's not actually a number.

    • #56392

      Olaf Soyk

      Wouldn't it work when you cast it to [VERSION]?

  • #56395

    Don Jones

    You'd have to cast both values – the Version property and the 6.2 comparison – to [version], but yes, that's what [version] is for.

  • #56407

    Rob Simmers

    The casting that is being referred to would be something like this:

    [version]$os.Version -gt [version]"6.2"

    Powershell will do type conversions for you in some cases, however, "strong-typed" code will not leave things to chance. You want to do comparisons to the same type string to string, int to int, etc. and in this case version to version. Casting attempts to convert the value to the type so that a proper comparison can be done. Even when I was playing with the version cast, it didn't like X.0:

    PS C:\Users\Rob> [version]10.0
    Cannot convert value "10" to type "System.Version". Error: "Version string portion was too short or too long."
    At line:1 char:1
    + [version]10.0
    + ~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastParseTargetInvocation
    PS C:\Users\Rob> [version]"10.0"
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    10     0      -1     -1      

    It's especially important when doing comparisons like -gt, because you are basically doing something like TEN -gt 6.2.

  • #56482


    exactly what i was looking for , the [version] cast , didn't know about it before
    nice find 🙂

    PS C:\> [version]$os.version -gt [version]"6.2"

You must be logged in to reply to this topic.