Identifying Windows Version (Part 3)

by Jun 18, 2021

In the previous tip we illustrated that accessing the WinRT class AnalyticsInfo seems to be the only supported way to read the current Windows 10 version. Rather than using asynchronous methods like in the previous example, to get just the current Windows 10 version, here is a simpler way:

# get raw Windows version
[int64]$rawVersion = 
  GetMember('get_VersionInfo').Invoke( $Null, $Null ).DeviceFamilyVersion

# decode bits to version bytes
$major = ( $rawVersion -band 0xFFFF000000000000l ) -shr 48
$minor = ( $rawVersion -band 0x0000FFFF00000000l ) -shr 32
$build = ( $rawVersion -band 0x00000000FFFF0000l ) -shr 16
$revision =   $rawVersion -band 0x000000000000FFFFl

# compose version
$winver = [System.Version]::new($major, $minor, $build, $revision)

Note that PowerShell 7 (pwsh.exe) cannot access this API. The code requires Windows PowerShell (powershell.exe).

Twitter This Tip! ReTweet this Tip!