Part of troubleshooting a Windows computer is finding out the operating system version. A typical user would know to check the computer’s properties to see the Windows version and build, similar to the one below.
Alternatively, you can execute the winver command and get the below result.
But what if you don’t have access to the GUI? Perhaps you need to get the same information from a remote computer? That’s where the awe of PowerShell shines through.
How do we get the Windows version the PowerShell way? Let’s explore several ways.
Table of Contents
Method 1: PowerShell Get OS Version from the Registry
Would you be surprised that the Windows operating system information can be found inside the registry? Probably not. You can find that information in this location in the registry.
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion
Viewing the Windows OS version in the registry editor is fine, but that’s not what we’re learning here. So, how do we perform the PowerShell get OS version from the registry method?
Open PowerShell and run this Get-ItemProperty command.
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
But as you can see above, the command returns many properties we don’t need. So let’s clean it up by selecting only the properties we want.
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | ` Format-List ProductName,DisplayVersion,CurrentBuildNumber
Isn’t this a lot better?
Note. Take a look at our article HTTP/HTTPS requests via Invoke-WebRequest PowerShell cmdlet.
Method 2: Query the Win32_OperatingSystem Class (WMI)
Through WMI classes, you can query different information about the local or remote computer. One of the details you can pull from WMI is the computer’s operating system version through the Win32_OperatingSystem or CIM_OperatingSystem class.
You can query WMI classes using the Get-WmiObject or Get-CimInstance cmdlets. There are differences between these two cmdlets that are not relevant to this task, but both will achieve the same goal.
Note. Get-CimInstance is the recommended and modern cmdlet.
# Windows PowerShell Get-WmiObject Win32_OperatingSystem | ` Format-List Caption, Version, BuildNumber
# PowerShell Core Get-CimInstance CIM_OperatingSystem | ` Format-List Caption, Version, BuildNumber
If you want to query a remote computer instead, you must append the -ComputerName parameter followed by the remote computer name.
# Windows PowerShell Get-WmiObject Win32_OperatingSystem -ComputerName <COMPUTER_NAME> | ` Format-List Caption, Version, BuildNumber # PowerShell Core Get-CimInstance CIM_OperatingSystem -ComputerName <COMPUTER_NAME> | ` Format-List Caption, Version, BuildNumber
Method 3: PowerShell Show Version from the System.Environment Class (.NET)
PowerShell can expose .NET classes and call their static methods. For example, the System.Environment class has an OSVersion property that returns the current operating system version.
Run the below command in PowerShell to get the Version property.
[System.Environment]::OSVersion.Version
Unfortunately, this method only shows the platform version and build numbers. The result does not say the friendly name (ie. Windows 11, Windows 10, etc.). Like in this example, the build number 19044 translates to Windows 10 20H2.
Refer to the List of Microsoft Windows versions.
This method may not be the best option if you care to retrieve the operating system name instead of only the version numbers.
Method 4: PowerShell Check Windows Version with the Get-ComputerInfo Cmdlet
The Get-ComputerInfo cmdlet is a built-in cmdlet that returns various details about the local computer. If you run it on its own, you’ll get a result similar to the one below.
Note. Check our guide on how to use PowerShell to check the status of a Windows service with Get-Service cmdlet.
But since we’re only interested in getting the Windows OS version, let’s append the -Property parameter with the specific properties we need.
Get-ComputerInfo -Property OSName,OSVersion
And that’s how you get the OS version the PowerShell way.
Method 5: PowerShell Check Windows Version with SystemInfo Command
Like the Get-ComputerInfo cmdlet, the systeminfo.exe command is a native command that returns the computer information, too.
But since systeminfo.exe is not a PowerShell cmdlet, it doesn’t produce the result as an object, so we can’t select specific properties from it. Instead, we can pass the result to the Select-String cmdlet to filter the OS Name and OS Version lines.
systeminfo.exe | Select-String "\bOS Name","\bOS Version"
Note. The \b switch is a regular expression to set a word boundary for the match. Without it, the result would also match BIOS because it has the OS characters.
Conclusion
You’ve now learned the many ways to get the Windows version in PowerShell. You see how to use built-in cmdlets, native commands, .NET classes, and the registry to retrieve the current Windows version.
2 comments
most of these dont work on win 11 unless you focus on the minor version, the registry check should be used first to determine if its 7 then you can move forward to checking the major/minor(major only needed for 8. Its much faster to split the task than to wait for systeminfo.exe
typos galore, forgot to mention, the second check is fastest if you use wmi and only use caption