Home > other >  PowerShell, determine if we are running on Windows 10 or Windows 11
PowerShell, determine if we are running on Windows 10 or Windows 11

Time:10-31

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 ?

CodePudding user response:

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.

  • Related