Determine OS Version (2016/2019)

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.

Is it possible that you accidentally queried your local client instead of a server? :wink: The server versions report their version usually correct.

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

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.

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

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.

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])

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’