Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET!
PowerShell

Getting Operating System Information with PowerShell

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 Content

Read the Best Personal and Business Tech without Ads

Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.

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.

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

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.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.

RSVP Now

Sponsored By