Determine OS Version (2016/2019)

Welcome Forums General PowerShell Q&A Determine OS Version (2016/2019)

Viewing 6 reply threads
  • Author
    Posts
    • #184889
      Participant
      Topics: 7
      Replies: 5
      Points: 88
      Rank: Member

      Hi All,

      My org is looking at rolling out 2019 in the near future and a number of our scripts will need to behave differently depending on which operating system they are run on.

      Currently to differentiate between 2012 and 2016 we make use of the OS Version

      (Get-CimInstance Win32_OperatingSystem).Version

      Unfortunately it appears that both windows 2016 and 2019 report back version 10.

      Does anyone know if MS has a recommended or preferred method of testing the OS version through PowerShell?Ā  I have spent some time on searching but the best I can find is the caption provided by

      (Get-CimInstance Win32_OperatingSystem).caption

      Thanks in advance for any help.

    • #184964
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      Is it possible that you accidentally queried your local client instead of a server? šŸ˜‰ The server versions report their version usually correct.

    • #184979
      Participant
      Topics: 7
      Replies: 5
      Points: 88
      Rank: Member

      Hi Olaf,

      No, I have run this test several times but I may not have explained it correctly when I said version 10, that is just the major version, I omitted the minor/build/revision.

      I haveĀ a vauge memory of reading an article explaining how the Win32_OperatingSystem version returns the underlying NT version and not the actual OS but I have not been able to find the article again.

      Server 1 – 2016

      PS C:\Users\Administrator> (get-ciminstance Win32_OperatingSystem).version
      10.0.14393
      PS C:\Users\Administrator> (get-ciminstance Win32_OperatingSystem).caption
      Microsoft Windows Server 2016 Standard

      PS C:\Users\Administrator> [system.environment]::osversion.Version

      Major Minor Build Revision
      —– —– —– ——–
      10 0 14393 0

       

       

      Server 2 – 2019

      PS C:\Users\Administrator> (get-ciminstance Win32_OperatingSystem).version
      10.0.17763

      PS C:\Users\Administrator> (get-ciminstance Win32_OperatingSystem).caption
      Microsoft Windows Server 2019 Standard

      PS C:\Users\Administrator> [system.environment]::osversion.Version

      Major Minor Build Revision
      —– —– —– ——–
      10 0 17763 0

       

      For reference, from my notesĀ previous version of windows are:

      2012r2 – 6.3

      2012r1 – 6.2

      2008r2 – 6.1

      2008r1 – 6.0

      Due to the previous changes in Major/Minor version with each release that is all our code is structed to handle, as such when we hit 2019 which has the same major/minor as 2016, the code is failing to differentiate.

      I figured this was the perfect time to try and identify a cleaner more generic solution, I say generic as I have no idea how or when these version numbers, specifically the build/revision will change, or when.Ā  I do not wish to find an update changes these numbers thus breaking the code all over again.

      The best I have come up with some far is to extract OS version from the caption.

    • #185042
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      Since the kernel of Windows 10 an Windows Server is actually the same you might take more than just the major version to differenciate between the different versions. If you're actually just trying to get the Windows Version what's wrong with going with the caption?

      ... some more stuff to read about List of Microsoft Windows versions or ... Operating System Version

    • #185138
      Participant
      Topics: 4
      Replies: 95
      Points: 192
      Helping Hand
      Rank: Participant

      True true false!

      2019 is new long term version of 2016 server so the queries are accurate.
      "10 0 14393 0" != "10 0 17763 0"

      I haven't seen any scripts that would not work with 2019 which are working with 2016, so check first if you actually need to differentiate them or not. I bet you don't.

    • #185243
      Senior Moderator
      Topics: 8
      Replies: 1141
      Points: 3,927
      Helping Hand
      Rank: Community Hero

      to have a full comparison. Never used it in this way !

      ([System.Environment]::OSVersion.Version).toString().replace('.','') -gt ('10.0.17154.0'.replace('.','') -as [int])
      
    • #185249
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      You don't need to do string acrobatics to compare version numbers. Powershell is able to handle that gracefully:

      (Get-CimInstance -ClassName CIM_OperatingSystem).Version -ge [Version]'10.0.18362'
Viewing 6 reply threads
  • You must be logged in to reply to this topic.