PowerShell One-Liner to Query multiple WMI Classes with a CIM Session

Today I thought I would share a one-liner that I recently wrote to query the Manufacturer, Model, and Serial Number information from numerous remote servers. Sounds simple enough, right?

This one-liner starts out by using my New-MrCimSession function to create a CIM session to each of the specified servers. This function automatically determines if the remote server supports the WSMan protocol and falls back to DCom if it doesn't:

1Get-CimSession | Select-Object -Property Name, ComputerName, Protocol


Two different WMI classes are needed to retrieve the necessary information. The manufacturer and serial number can be retrieved from the Win32\_BIOS class and the model can be retrieved from the Win32\_ComputerSystem class. Retrieving this information with a script or function and combining the results from both classes into a custom object would be simple, but it's a little more challenging to retrieving this information with a one-liner.

1New-MrCimSession -ComputerName sql02, sql03, sql04, web01 -Credential (Get-Credential) |
2Get-CimInstance -ClassName Win32_BIOS -Property Manufacturer, SerialNumber |
3Select-Object -Property PSComputerName, Manufacturer,
4                        @{label='Model';expression={(Get-CimInstance -CimSession (
5                            Get-CimSession | Where-Object ComputerName -eq $_.PSComputerName
6                            ) -ClassName Win32_ComputerSystem -Property Model).Model}},
7                        SerialNumber


The challenging part is reusing the CIM session with Select-Object to query the second WMI class while creating a custom object with a one-liner.

Be sure to remove those CIM sessions when you're finished:

1Get-CimSession | Remove-CimSession


The CIM cmdlets were first introduced with PowerShell version 3. The computer you're performing the query from must have at least PowerShell version 3 installed, but the remote computer that the query is being run against doesn't need PowerShell installed at all since using Get-CimInstance with a CIM session allows it to fall back to using the DCom protocol.

The New-MrCimSession function shown in the blog article can be downloaded from my PowerShell repository on GitHub or installed as part of my MrToolkit module from the PowerShell Gallery.