9

If I run the following version test on Windows 10 or Windows 11, they both report $Major as 10 and $Minor as 0, so this test is not sufficient to determine if we are running on Windows 10 or Windows 11.

[version]$OSVersion = [Environment]::OSVersion.Version $Major = $OSVersion.Major $Minor = $OSVersion.Minor # Other ways to test: # $OSVersion = [Version](Get-ItemProperty -Path "$($Env:Windir)\System32\hal.dll" -ErrorAction SilentlyContinue).VersionInfo.FileVersion.Split()[0] # [version]$OSVersion = Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty Version 

In PowerShell, how can we distinguish if we are running in Windows 10 or Windows 11 ?

2 Answers 2

6

Using WMI data directly requires less overhead

$isWin11 = (Get-WmiObject Win32_OperatingSystem).Caption -Match "Windows 11" 

Alternative with CIM:

$isWin11 = (Get-CimInstance -Class Win32_OperatingSystem).Caption -Match "Windows 11" 
Sign up to request clarification or add additional context in comments.

2 Comments

It's a small point (essentially the same answer), but technically I guess that @Toni's answer below is probably the preferred method since WMI is more and more frowned upon, and we are encouraged to use CIM. If (get-ciminstance -query "select caption from win32_operatingsystem where caption like '%Windows 11%'") {1}
Just tested and get-ciminstance -query "select caption from win32_operatingsystem" doesn't work, answer updated with alternative working CIM method.
4

On Wikipedia you can find a list of build numbers and the information to which operating system version they belong. Using this information, you can determine the OS Version by comparing the [Environment]::OSVersion.Version.Build property.


Also, the Get-ComputerInfo cmdlet returns you the OSName as a string like that:

Microsoft Windows 11 Pro

You could use the -match operator to check whether the string contains "11":

(Get-ComputerInfo | Select-Object -expand OsName) -match 11 

This should work in most cases, but I doubt that this would be the best option.

3 Comments

Building a list from build numbers sounds like something that would fail over time as new build numbers appear, but thanks for the Get-ComputerInfo approac, that works great, but it does seem rather slow as it has to process everything in the Get-ComputerInfo details (I guess this is doing the same as the systeminfo command). If we can find the exact thing that it is doing to generate the OsName that would be perfect. I did a full search in the registry for "Microsoft Windows 10 Pro" but that text is not present.
to get the information in a more specific way you could do: get-ciminstance -query "select caption from win32_operatingsystem" or to use it in a If statement: If (get-ciminstance -query "select caption from win32_operatingsystem where caption like '%Windows 11%'"){}
One could do get-computerinfo -Property OsName but strangely enough, this still queries many unrelated stuff like CPU and network information :(.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.