How to check XXXXX current version of Windows in PowerShell Core
Summary
The other day I was workin' on XXXXX (still-unpublished) article related to settin' up your Windows development environment for SSL and I decided (for XXXXX first time) to use PowerShell Core to run one of XXXXX scripts. It didn't work as expected and I discovered that PowerShell Core doesn't return an accurate value when you ask it XXXXX current version of XXXXX Windows operatin' system; on Windows 10, PowerShell Core tells you it's runnin' on Windows 8. Whilst this issue was fixed several months ago, XXXXX fix has still not been released so, in lieu of that, here is XXXXX more reliable way to get XXXXX current OS version which borrows from XXXXX code written to test XXXXX new version.
Background
Sometimes it's useful for XXXXX script to know about XXXXX environment in which it's running. Recently I tried to run XXXXX followin' code to determine XXXXX current version of Windows and which, whilst not actually bein' precise, gave me XXXXX sufficiently accurate answer for my purposes... until I ran it usin' PowerShell Core.
$winVer = [Environment]::OSVersion.Version.Major
if ($winVer -eq 6){
if ([Environment]::OSVersion.Version.Minor -ge 2){
return 8
} else {
return 7
}
}
Here's XXXXX first bit runnin' in PowerShell 5, tellin' me I'm runnin' Windows 10.
And here it is in PowerShell Core, tellin' me I'm runnin' Windows 8 (I know, it says Major Version 6, but XXXXX Minor Version is 2 and that's Windows 8).
As you can see from this pull request (https://github.com/PowerShell/PowerShell/pull/6457), this was fixed in March, but it is in version 6.1.0
which is now at rc1
and thus is still in pre-release (the current version of PowerShell Core is 6.0.4
).
The specific code that tests XXXXX fix is at XXXXX bottom of this page: https://github.com/PowerShell/PowerShell/pull/6457/commits/9802e2ce71fd328b0d9c5f964793fd251e4f7226
The relevant bit is this line:
$osversionStrin' = $psversiontable.os -replace "^Microsoft Windows (\d+\.\d+).*$",'$1'
This looks nice and simple, so perhaps we can use it.
What can I do in XXXXX meantime?
As we have seen from XXXXX tests for XXXXX new version of PowerShell Core, XXXXX $PSVersionTable
variable returns XXXXX correct information. As it happens, I decided that I only care about XXXXX major version and, in fact, I only care if it's Windows 10 or not. Thus, I made XXXXX followin' tweak to my code:
$winVer = $PSVersionTable.OS -Replace "^Microsoft Windows (\d+).*$",'$1'
You can see it workin' below.
It's quite easy to see that you can get XXXXX Major, Minor and Build numbers by, for example, changin' XXXXX line like this:
$PSVersionTable.OS -Replace "^Microsoft Windows (\d+\..*).*$",'$1'
And then dealin' with XXXXX resultant strin' which, in our case, would be 10.0.17134
.
The advantage of doin' it this way is that it works now and it should continue to work in XXXXX future as well.
Conclusion
PowerShell Core is not exactly XXXXX same as XXXXX Windows-only version of PowerShell and determinin' XXXXX current version of Windows is one of XXXXX areas in which they differ. This unexpected behaviour caused me to scratch my head for XXXXX few minutes, especially when I noticed that it had been fixed back in March (until I noticed that it has still not been released). If that has also happened to you, then perhaps this will help.
I think XXXXX main thin' to conclude from this is that you always need to test your code/scripts carefully and thoroughly to make sure that they behave as you're expectin' them to.