When managing a Windows environment, understanding the exact version of the operating system in use is critical for patching, compatibility, and security. PowerShell provides a robust set of cmdlets to retrieve this information directly from the command line, allowing for automation and remote querying. This guide details the methods to obtain the operating system version using PowerShell, covering everything from basic commands to parsing complex object properties.
Using Get-CimInstance for Detailed Information
The most reliable and modern approach to gathering OS data is by using Get-CimInstance . This cmdlet queries the Common Information Model (CIM), which is the preferred interface over the older WMI because it uses WinRM and is more efficient. To fetch the operating system details, you target the Win32_OperatingSystem class.
Running Get-CimInstance -ClassName Win32_OperatingSystem returns an object rich with properties. You will receive the Caption, Version, BuildNumber, OSArchitecture, and InstallDate. This method is favored in production scripts because it is stable and provides structured data that is easy to filter and format.
Extracting the Version Number
The "Version" property returned by the cmdlet is a string that represents the build number, such as "10.0.19041" or "6.3.9600". While this identifies the specific build, it does not provide the user-friendly marketing name like "Windows 10 Pro" or "Windows Server 2022". To get the descriptive caption, you access the Caption property. Combining these two properties gives you both the technical identity and the common name of the OS.
Leveraging Get-ComputerInfo for Simplicity
For ad-hoc checks and simpler scripts, Get-ComputerInfo offers a high-level overview without needing to specify a class name. This cmdlet aggregates data from various sources and presents it in a flat structure. It is particularly useful for quickly verifying the OS edition and version without delving into the CIM object properties.
Querying Remote Machines
PowerShell's strength lies in its ability to manage remote systems. To check the OS version on another computer, you can use the -CimSession parameter. You first create a session using New-CimSession -ComputerName "Server01" and then pass that session to Get-CimInstance . This is essential for enterprise environments where audits and compliance checks need to be performed across dozens or hundreds of machines.
Ensure that the target machine has PowerShell remoting enabled and that your account has administrative privileges. Network firewalls must also allow traffic on port 5985 (HTTP) or 5985 (HTTPS) for the CIM session to establish successfully. Filtering and Formatting Output Raw data is only useful if you can interpret it. PowerShell allows you to filter results using Where-Object to find machines matching specific criteria. For example, you might search for all machines running a version older than a specific build number to prioritize updates.
Filtering and Formatting Output
Formatting the output as a table or list makes the data human-readable. Using Format-Table -AutoSize ensures that the columns adjust to the content. This is vital when exporting data to a text file for documentation or sharing with team members who may not have access to the PowerShell console.