Win10 vs Win 8.1 os version

Welcome Forums General PowerShell Q&A Win10 vs Win 8.1 os version

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
2 years, 2 months ago.

  • Author
    Posts
  • #56386

    Participant
    Points: 35
    Rank: Member

    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

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    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

      Participant
      Points: 419
      Helping Hand
      Rank: Contributor

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

  • #56395

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    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.

    • #56405

      Participant
      Points: 419
      Helping Hand
      Rank: Contributor

      Cool ... thanks

  • #56407

    Participant
    Points: 438
    Helping Hand
    Rank: Contributor

    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

    Participant
    Points: 35
    Rank: Member

    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

The topic ‘Win10 vs Win 8.1 os version’ is closed to new replies.