Win10 vs Win 8.1 os version

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Alexandru Alexandru 1 month ago.

  • Author
    Posts
  • #56386
    Profile photo of Alexandru
    Alexandru
    Participant

    Hi,
    Any idea why this is happening ?
    On a windows 10

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

    While in Win 8.1

    PS C:\> $os = Get-WmiObject win32_operatingsystem
    PS C:\> $o.version -gt 6.2
    True
  • #56389
    Profile photo of Don Jones
    Don Jones
    Keymaster

    See the documentation at https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx.

    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
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

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

  • #56395
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    Profile photo of Alexandru
    Alexandru
    Participant

    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"
    True

You must be logged in to reply to this topic.