Create Summary Objects for Inventory

by Aug 25, 2017

Beginning in PowerShell 3, a PSCustomObject can easily combine valuable information that you gathered from different sources. Here is an example that gets various information from different WMI classes, and produces one inventory object that can then be passed on or worked with:

# get information from this computer
$Computername = "."

# get basic information (i.e. from WMI)
$comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computername 
$bios = Get-WmiObject -Class Win32_bios -ComputerName $Computername
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computername


# combine everything important in one object
[PSCustomObject]@{
    ComputerName = $Computername
    Timestamp = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss') 
    Model = $comp.Model
    Manufacturer = $comp.Manufacturer
    BIOSVersion = $bios.SMbiosbiosversion
    BIOSSerialNumber = $bios.serialnumber
    OSVersion = $os.Version
    InstallDate = $os.ConvertToDateTime( $os.InstallDate)
    LastBoot = $os.ConvertToDateTime($os.lastbootuptime)
    LoggedOnUser = $Comp.UserName
}

Twitter This Tip! ReTweet this Tip!