Saw a question about testing the operating system and decided that the easiest way was to use the Version property from Win32_OperatingSystem. By easiest I mean least typing and string testing.
$os = Get-WmiObject -Class Win32_OperatingSystem
switch ($os.Version) {
{$_ -like ‘10.0*’} {$osv = “Windows 10”; break}
{$_ -like ‘6.3*’} {$osv = “Windows 8.1”; break}
{$_ -like ‘6.2*’} {$osv = “Windows 8”; break}
{$_ -like ‘6.1*’} {$osv = “Windows 7”; break}
default {$osv = “Unrecognised OS”}
}
$osv
I used Get-WmiObject as Windows 7 comes with PowerShell v2 which doesn’t contain the CIM cmdlets. I also restricted the test to supported operating systems.
A switch based on the Version property of Win32_OperatingSystem does the hard work. Use a string comparison using –like for the test.
Finish by printing the result.
If you want to include server operating systems you’ll need to also test the caption property.