Getting Operating System Information with PowerShell
In today’s Ask the Admin, I’ll show you how to get basic OS information using PowerShell.
Whether you’re an experienced script writer or dipping your toes into PowerShell for the first time, it’s often important to understand something about the version of the operating system that you are dealing with before running your code. The cmdlets that I’m going to show you in this article can be used standalone, i.e. so you don’t have to go searching in the UI for specific information, or as part of your scripts on which you can base logic.
There are two PowerShell cmdlets for extracting OS information, Get-WMIObject and Get-CimInstance. WMI is Microsoft’s implementation of the Common Information Model (CIM), and while many administrators are more likely to be familiar with Windows Management Instrumentation (WMI) as a concept, I recommend using Get-CimInstance because there’s a wider variety of CIM cmdlets available that can provide more information and flexibility.
Using PowerShell’s Get-CimInstance
Let’s start by running the Get-CimInstance and specifying the Win32_OperatingSystem class:
Get-CimInstance -ClassName Win32_OperatingSystem
To get the complete list of information available in the class, pipe the results to the Format-List cmdlet as shown below. Note that I’m able to leave out -ClassName and Win32_OperatingSystem becomes a positional parameter; though while not a best practice, this does reduce the amount of typing to be done.
Get-CimInstance Win32_OperatingSystem | Format-List *
If you do intend to use these cmdlets on a regular basis, they can be shortened further as shown here, but for the purposes of this article I’ll continue to use the full cmdlet names.
gcim Win32_OperatingSystem | fl *
Now that we can see the full list of properties, let’s pipe the results to the Select-Object cmdlet to pick exactly the information required:
Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, ServicePackMajorVersion, OSArchitecture, CSName, WindowsDirectory
If you want to use the information as part of a script, you can add the results to an array:
$osInfo = Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, ServicePackMajorVersion, OSArchitecture, CSName, WindowsDirectory
You can then process the information as required and if necessary, extract individual properties, such as $osInfo.OSArchitecture or $osInfo.CSName, which give the operating system architecture, 32 or 64-bit, and computer name respectively.
The Get-CimInstance cmdlet can also be used to talk to one or more remote computers:
Get-CimInstance Win32_OperatingSystem -ComputerName dc1, dc2
In this article, I showed you how to use the PowerShell Get-CimInstance cmdlet to get operating system information, such as the OS architecture and OS name, and how to list, format, and add the results to a variable or array.
More in PowerShell
How to Create Files and Folders with PowerShell
Jun 22, 2022 | Mike Kanakos
How to Use PowerShell For Loop, While Loop, and Other Loops
May 23, 2022 | Ivan Mirchev
How to Check Your PowerShell Version
May 4, 2022 | Michael Reinders
How to Download a File using PowerShell
Apr 21, 2022 | Michael Reinders
How to Write and Run a PowerShell Script File on Windows 11
Apr 4, 2022 | Ivan Mirchev
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri