3

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 ?

YorSubs
  • 3,194
  • 7
  • 37
  • 60

2 Answers2

2

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.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • 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. – YorSubs Oct 30 '22 at 09:13
  • 2
    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%'"){}``` – Toni Oct 30 '22 at 09:17
  • One could do `get-computerinfo -Property OsName` but strangely enough, this still queries many unrelated stuff like CPU and network information :(. – zett42 Oct 30 '22 at 13:16
1

Using WMI data directly requires less overhead

$isWin11 = (Get-WmiObject Win32_OperatingSystem).Caption -Match "Windows 11"
NiKiZe
  • 1,256
  • 10
  • 26