Combining multiple WMI query into a PowerShell object

Hello,

If you red some of my previous article on WMI, you probably said yourself “That’s nice, I can make a great report with it, but I need to output all of those informations on a single line for each computer.”, that’s totally possible. You need to create a custom PowerShell object, and store all of the needed properties in it.

There are several way of creating an object, mostly depending of PowerShell version :

  1. V1 : PowerShellObjectV1
  2. V2 : PowerShellObjectV2
  3. V3 : PowerShellObjectV3

You’ll notice if you make those on your own that the V2 way, sort your property in a pretty mystic way, that’s why in V3 we can use the accelerator [Ordered] to keep the hashtable order.

Now you know how to make a PowerShell object, let’s use it :

$bios = Get-WmiObject Win32_BIOS -ComputerName localhost
$Proc = Get-WmiObject Win32_processor -ComputerName localhost | Select-Object -First 1
$memory = Get-WmiObject Win32_physicalmemory -ComputerName localhost
$system= Get-WmiObject Win32_ComputerSystem -ComputerName localhost

$Object = New-Object PSObject -Property @{
ComputerName           = $proc.SystemName
Manufacturer           = $bios.Manufacturer
Model                  = $system.Model
'BIOS Version'         = $bios.Version
'Serial Number'        = $bios.SerialNumber
'Processor Number'     = $system.NumberOfProcessors
'Processor Name'       = $proc.name
'Logical Processeur'   = $system.NumberOfLogicalProcessors
'Speed (MHZ)'          = $proc.CurrentClockSpeed
'RAM (GB)'             = $system.TotalPhysicalMemory / 1GB -as [int]
'Used RAM slot'        = $memory.count
}
Write-Output $Object

Gwmi-CombineObject

You need to know that when you output an object, the object is in the pipeline, so you can pipe the output in whatever you want such as “Select-Object“, “Export-CSV“,”Format-Table“, or “Out-GridView“.

Leave a Reply