How to Get the Windows Version in PowerShell
-
Using the
[System.Environment]
Class in PowerShell to Get the Windows Version -
Using the
Get-ComputerInfo
Cmdlet in PowerShell to Get the Windows Version -
Using the WMI Class With
Get-WMIObject
Cmdlet in PowerShell to Get the Windows Version -
Using the
systeminfo
Legacy Command PowerShell to Get the Windows Version -
Using the
Get-CimInstance
Legacy Command PowerShell to Get the Windows Version - Conclusion
The fastest way to get which Windows operating system your computer has is to use the winver
command. In Windows PowerShell, there are multiple ways to get your Windows version operating system, and we will discuss them here in this article.
Various methods exist to accomplish this, each with its advantages and use cases. In this article, we’ll explore five different approaches to get the Windows version in PowerShell.
Each method offers unique insights into the Windows environment, providing administrators and scripters with versatile tools to gather essential system information.
Using the [System.Environment]
Class in PowerShell to Get the Windows Version
In PowerShell, you can retrieve detailed information about the Windows version using the [Environment]::OSVersion.Version
method. This method accesses the static Version
property of the OSVersion
property in the Environment
class, providing a straightforward way to access the operating system version.
Example Code:
[System.Environment]::OSVersion.Version
When we use [Environment]::OSVersion.Version
, we’re accessing the OSVersion
property of the Environment
class, which provides information about the operating system environment. Specifically, we’re retrieving the Version
property of this object, which contains details about the Windows version.
This method returns a System.Version
object, which represents the version of the operating system as a combination of major and minor version numbers. By accessing the Major
and Minor
properties of this object, we can extract these version numbers and use them as needed in our PowerShell script.
Output:
We may refer to the official Microsoft Document to cross-reference the current Windows version operating system that you are currently running.
However, this will not show the correct version if you’re using the newest operating system, like Windows 11 or Windows Server 2019, as it will still show a Major build 10, which represents Windows 10 and Windows Server 2016. Therefore, the command above will only show proper values if you run Windows 10 and Windows Server 2016 below.
Using the Get-ComputerInfo
Cmdlet in PowerShell to Get the Windows Version
In PowerShell, you can easily retrieve detailed information about the Windows operating system using the Get-ComputerInfo
cmdlet. This cmdlet gathers information about the local computer system, including the operating system name, version, and hardware abstraction layer (HAL).
Example Code:
Get-ComputerInfo | Select-Object OSName, OSVersion, OsHardwareAbstractionLayer
When we use the Get-ComputerInfo
cmdlet followed by piping it to Select-Object
, we’re retrieving comprehensive information about the local computer system. By specifying the properties OSName
, OSVersion
, and OsHardwareAbstractionLayer
, we’re selecting specific details about the operating system, such as its name, version, and hardware abstraction layer (HAL).
This method allows us to gather detailed information about the Windows environment, which can be useful for various administrative tasks, troubleshooting, or scripting purposes. By accessing and displaying these properties, we gain insights into the configuration and specifications of the Windows system, aiding in system management and maintenance.
Output:
Using the WMI Class With Get-WMIObject
Cmdlet in PowerShell to Get the Windows Version
We may also use the Windows Management Instrumentation (WMI) class to check for the current version of your operating system.
Example Code:
(Get-WmiObject -class Win32_OperatingSystem).Caption
When we execute (Get-WmiObject -class Win32_OperatingSystem).Caption
, we’re utilizing the Get-WmiObject
cmdlet to query Windows Management Instrumentation (WMI) for information about the operating system. Specifically, we’re targeting the Win32_OperatingSystem
class, which holds details about the operating system.
By accessing the .Caption
property of the resulting object, we’re retrieving the name of the operating system. This method offers a straightforward approach to obtaining the Windows version information directly through PowerShell, making it convenient for various scripting and administrative tasks.
Output:
Unlike the [System.Environment]
class and Get-ComputerInfo
cmdlet, the WMI object correctly displays the Windows operating system version if you’re using the latest version.
Using the systeminfo
Legacy Command PowerShell to Get the Windows Version
We can also use the systeminfo
legacy command with Windows PowerShell cmdlet wrappers to output the detailed operating system version. By combining systeminfo
with PowerShell cmdlets, you can extract specific information about the Windows version.
systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List
When we execute the command systeminfo /fo csv
, we’re utilizing the systeminfo
command-line tool to gather detailed system information in CSV format.
Then, we use ConvertFrom-Csv
to convert the CSV-formatted output into PowerShell objects. By piping the result into Select
, we filter the properties we’re interested in, specifically those starting with OS
, System
, and Hotfix
.
Finally, we apply Format-List
to present the information in a formatted list view.
Output:
Using the Get-CimInstance
Legacy Command PowerShell to Get the Windows Version
This cmdlet is part of the Common Information Model (CIM) infrastructure in PowerShell, allowing you to query system information in a standardized way. By targeting the Win32_OperatingSystem
class, you can access properties such as the operating system name and version.
(Get-CimInstance Win32_OperatingSystem) | Select-Object Caption, Version
When we execute (Get-CimInstance Win32_OperatingSystem) | Select-Object Caption, Version
, we’re utilizing the Get-CimInstance
cmdlet to retrieve information about the Windows operating system from the Win32_OperatingSystem
class. This class represents various properties of the operating system. By piping the result into Select-Object
, we specify the properties we’re interested in, which are Caption
(representing the name of the operating system) and Version
.
Output:
Conclusion
In PowerShell, obtaining the Windows version is essential for system administration and scripting tasks. We’ve explored five different methods to accomplish this: using the [System.Environment]
class, Get-ComputerInfo
cmdlet, Get-WmiObject
cmdlet, systeminfo
command, and Get-CimInstance
cmdlet.
Each approach offers distinct advantages and may be preferred depending on specific requirements and scenarios. Whether it’s accessing the version directly through the [System.Environment]
class or querying detailed system information using Get-ComputerInfo
or Get-CimInstance
, PowerShell provides robust tools for effectively managing and monitoring Windows environments.
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn