Within PowerShell there are several different options for returning the OS version in Windows 10:
Get-ComputerInfo OsName,OsVersion,OsBuildNumber,OsHardwareAbstractionLayer,WindowsVersion
This returns most of the version info you could need, but will not work in old versions of Windows, requires PowerShell 5.0+.
Read the build/releaseID (1803,1909, 20H2 etc) from the registry
For Windows 10 up to May 2020 / 2004, this can be retrieved via the ReleaseID key:
$release = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseIdFor all later versions, this can be retrieved via the DisplayVersion key:
$ver = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name DisplayVersion).DisplayVersionThe ReleaseID is now deprecated and will return '2009' for 20H2 and all subsequent versions of Windows.
If ($release -eq '2009') {$ver} Else {$release}
To make this a handy function:
Function get-build{ $release = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId $ver = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name DisplayVersion).DisplayVersion If ($release -eq '2009') {$ver} Else {$release}
PS C:\> [environment]::OSVersion.Version
10.0.19043.0The OSVersion variable works in all OS's but it only returns the version number, it does not return the build/releaseID.
Prior to Windows 8.1 it would return major and minor verions as separate properties.To get the OS version of a remote machine, you could run this same command with Invoke-Command
PS C:\> $ver = Invoke-Command -Computername computer64 -ScriptBlock {[Environment]::OSVersion.Version} PS C:\> $verHowever this is quite slow and requires setting up PS Remoting so a faster and easier alternative is to use CIM as described below.
Get-CimInstance Win32_OperatingSystem
Get-CimInstance works in all OS’s and requires PowerShell 3.0+
Get-CimInstance like all the CIM cmdlets is particularly useful for querying remote machines, just add the -computername option.
Example script(Get-WmiInstance is an older and much slower version which predates the CIM* cmdlets, Get-WmiInstance works in PowerShell 1.0 and 2.0 but is deprecated in v3.0+ and not available at all in PowerShell 6.0)
SystemInfo works in all OS’s but as a CMD utility you will have to parse the string output with FOR.
It is also very slow.
VER also works in all OS’s but as an internal CMD command it has to be run within CMD.
To capture the output you either have to test the errorlevel or parse the string output.
Directly importing system DLLs, typically kernel32.dll or ntdll and reading the version information. This works but requires rather a lot of code and there’s no guarantee those DLLs will be updated or even exist in future versions of Windows.
One extra postscript about using an environment variable, is that system wide variables can potentially be changed, this can be very useful for testing changes but in highly secure systems you may not want to rely on the value always being true.
“In any collection of data, the figure most obviously correct, beyond all need of checking, is the mistake” ~ Finagle's third law
PowerShell versions and OS compatibility.
Get-Host - Get the version of PowerShell.
Get-ComputerInfo - Get system and operating system properties.
Get-OSVersion.ps1 - Find the OS version on a remote machine.
CMD: VER - Display the current operating system version, with a list of version numbers.