Consolidating Information In An Object

by Jul 7, 2009

Sometimes, you may want to store information you gathered in different places into one single object and return this object to the caller. This is pretty easy once you realize that Select-Object can not only remove but also add custom properties to an object, or as Bart Simpson put it, "the vacuum blows and sucks at the same time".

In the next example, PowerShell retrieves information from two different WMI classes. It creates a custom object by expanding the simple data object "1" by two new properties. Then, it saves the WMI information into those properties and returns the object.

$result = 1 | Select-Object Version, Build

$result.Version = (Get-WmiObject Win32_BIOS).version
$result.Build = (Get-WmiObject Win32_OperatingSystem).BuildNumber


Note that you can sort and group and measure this object in just the same way as any other object.