header image

Testing the operating system

Posted by: | February 18, 2019 Comments Off on Testing the operating system |

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”}



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.

under: PowerShell

Comments are closed.