Retrieve Basic Operating System Information with PowerShell

PowerShell version 5.1 added a new cmdlet named Get-ComputerInfo which retrieves lots of information from the local computer. It can be wrapped inside of the PowerShell remoting Invoke-Command cmdlet to retrieve that same information from remote computers.

My biggest complaint with Get-ComputerInfo is that it takes forever to return the information and then when it does finally complete, it’s missing values for most of its properties. Also, if you’re going to wrap it inside of Invoke-Command, then all of your remote machines would need PowerShell 5.1 or higher installed.

Because of these shortcomings, I decided to write my own simple function to retrieve just the operating system information I wanted while relying on CIM sessions for remote connectivity so down level clients could also be queried.

One of the most difficult parts of writing this function was figuring out how to query the registry of a remote computer with a CIM session. I also didn’t want to retrieve the information from one remote computer at a time. Instead, I wanted to retrieve the information for all of the computers at once and then put the pieces together before returning the information. While this is a little more complicated than query one computer at a time, the added efficiency and reduced run-time makes it worth the effort.

Simply run the function with no parameters to retrieve information from the local computer.

Now, I’ll create CIM sessions to several different servers running different operating system versions and various versions of PowerShell.

Notice that by using my New-MrCimSession function, it automatically created all of the ones capable of using the WSMan protocol using it and the one that wasn’t using the DCom protocol. The SQL05 server runs Windows Server 2008 (non-R2) and does not have PowerShell installed at all.

The version number such as 1511, 1607, 1703, or 1709 will only show up for machines running Windows 10 or Server 2016 (or higher if you’re reading this in the future), but the neat thing is the Get-MrOSInfo function is able to query down level servers such as SQL05 that do not have PowerShell installed.

I went ahead and added PowerShell version 1.0 to my SQL05 server. When the same command is run again, it shows that server does indeed now have PowerShell 1.0 installed. That particular version of PowerShell does not include remoting or the ability to return the version with the $PSVersionTable built-in variable.

Both the Get-MrOSInfo and New-MrCimSession functions shown in this blog article can be downloaded from my PowerShell repository on GitHub.

µ

1 Comment

  1. maxitnz

    Thank you so much for this script, awesome way to learn more about PowerShell.

    Reply

Leave a Reply

%d bloggers like this: