Search code examples
windowspowershellversion

Why does the $PSVersionTable.BuildVersion output differs from that of [System.Environment]::OSVersion.Version?


Why does the $PSVersionTable.BuildVersion output :

PS C:\> $PSVersionTable.BuildVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      19041  2673


PS C:\>

differs from that of [System.Environment]::OSVersion.Version ? :

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      19045  0


PS C:\>

Which one is more reliable ?


Solution

  • I did a bit of digging, and the BuildVersion entry in $PSVersionTable in Windows PowerShell 5.1 comes from the System.Management.Automation.dll - the assembly containing all of PowerShell's language and engine features.

    In other words: it's the assembly version, not the Operating System version that's reflected in $PSVersionTable['BuildVersion'] - and they might not be aligned since the assembly may have been replaced by a security update outside the lifecycle updates to the OS itself and vice-versa.

    You can discover the assembly version directly like this:

    $SMAVersionInfo = Get-ItemPropertyValue -LiteralPath ([psobject].Assembly.Location) -Name VersionInfo
    $SMAProductVersion = [version]$SMAVersionInfo.ProductVersion
    

    $SMAProductVersion should now have the exact same value as $PSVersionTable['BuildVersion']