Getting Operating System Information with PowerShell

Posted on April 4, 2016 by Russell Smith in PowerShell with 0 Comments

powershell-hero-img

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.

Sponsored

Using PowerShell’s Get-CimInstance

Let’s start by running the Get-CimInstance and specifying the Win32_OperatingSystem class:

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.

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.

Get basic OS information using PowerShell (Image Credit: Russell Smith)

Get basic OS information using PowerShell (Image Credit: Russell Smith)



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:

If you want to use the information as part of a script, you can add the results to an array:

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:

Sponsored

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.

Sponsored