Hardware inventory of a computer as a single object
Five classes are enough for an identity card: Win32_ComputerSystem (model, memory), Win32_OperatingSystem, Win32_BIOS, Win32_Processor and Win32_DiskDrive. The function below queries them and returns a single object per machine. Without a parameter it works locally; with -CimSession it queries a remote machine.
function Get-HardwareInventory {
[CmdletBinding()]
param (
[Microsoft.Management.Infrastructure.CimSession]
$CimSession
)
$sessionParameter = @{}
if ($CimSession) {
$sessionParameter['CimSession'] = $CimSession
}
$computerSystem = Get-CimInstance -ClassName Win32_ComputerSystem @sessionParameter
$operatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem @sessionParameter
$bios = Get-CimInstance -ClassName Win32_BIOS @sessionParameter
$processor = @(Get-CimInstance -ClassName Win32_Processor @sessionParameter)
$disk = @(Get-CimInstance -ClassName Win32_DiskDrive @sessionParameter)
[PSCustomObject]@{
ComputerName = $computerSystem.Name
Manufacturer = $computerSystem.Manufacturer
Model = $computerSystem.Model
OperatingSystem = $operatingSystem.Caption
OSVersion = $operatingSystem.Version
BiosVersion = $bios.SMBIOSBIOSVersion
BiosDate = $bios.ReleaseDate
SerialNumber = $bios.SerialNumber
Processor = $processor[0].Name
CoreCount = ($processor | Measure-Object -Property NumberOfCores -Sum).Sum
MemoryGB = [math]::Round($computerSystem.TotalPhysicalMemory / 1GB, 1)
DiskCount = $disk.Count
DiskTotalGB = [math]::Round(($disk | Measure-Object -Property Size -Sum).Sum / 1GB)
}
}
Get-HardwareInventory
BiosDate is already a [datetime]: Get-CimInstance converts CIM dates. To inventory several computers:
$cimSession = New-CimSession -ComputerName 'PC01', 'PC02'
try {
foreach ($session in $cimSession) {
Get-HardwareInventory -CimSession $session
}
} finally {
Remove-CimSession -CimSession $cimSession
}
The result is an object: Export-Csv -Path inventory.csv -NoTypeInformation or ConvertTo-Json save it directly.