PowerShell by Example: System Information Cmdlets
Retrieving detailed hardware and operating system information using Get-ComputerInfo and CIM cmdlets with this code example for system auditing and monitoring.
Code
# Get consolidated system information (Windows only)
$info = Get-ComputerInfo
Write-Host "OS: $($info.OsName)"
Write-Host "Uptime: $($info.OsUptime)"
# Get specific properties to improve performance
Get-ComputerInfo -Property "OsVersion", "BiosSeralNumber", "CsProcessors"
# Query WMI/CIM for Operating System details (Cross-platform compatible)
$os = Get-CimInstance -ClassName Win32_OperatingSystem
Write-Host "Last Boot: $($os.LastBootUpTime)"
Write-Host "Free Memory: $($os.FreePhysicalMemory) KB"
# Query Logical Disks (Drive space)
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object DeviceID, VolumeName, @{
Name = "FreeGB"
Expression = { [math]::Round($_.FreeSpace / 1GB, 2) }
}Explanation
PowerShell provides robust tools for auditing and gathering system information, primarily through the Get-ComputerInfo cmdlet and the Common Information Model (CIM) cmdlets. Introduced in PowerShell 5.1, Get-ComputerInfo is a comprehensive cmdlet that returns a consolidated object containing hundreds of system properties, including OS version, BIOS details, and hardware specifications. While extremely convenient for local snapshots, it is Windows-specific and can be performance-intensive if run without parameters. To optimize performance, it is best practice to use the -Property parameter to retrieve only the specific data points needed.
For cross-platform compatibility and remote management, Get-CimInstance is the modern standard, replacing the older and deprecated Get-WmiObject. It interacts with the Windows Management Instrumentation (WMI) database to query standard classes like Win32_OperatingSystem, Win32_LogicalDisk, and Win32_Service. CIM cmdlets use WS-Man for remote communication, making them more firewall-friendly than the DCOM-based WMI cmdlets. They return lightweight objects that are easier to serialize and transmit over the network, making them ideal for enterprise-scale monitoring and inventory scripts.
Understanding the distinction between these tools is crucial for writing efficient scripts. Use Get-ComputerInfo when you need a quick, human-readable summary of a local Windows machine's configuration. Switch to Get-CimInstance when building robust automation tools that need to run against remote servers, support older PowerShell versions, or require specific WMI class data not exposed by the higher-level computer info cmdlet. Both approaches allow for the creation of detailed system reports and health checks essential for proactive infrastructure management.
- Use
Get-ComputerInfofor quick local system snapshots - Optimize performance by selecting specific properties
- Prefer
Get-CimInstancefor remote and cross-platform queries - Calculate derived values like free disk space in GB
Code Breakdown
Get-ComputerInfo retrieves a comprehensive object with all system properties.-Property limits the output to specific fields, improving execution speed.Get-CimInstance queries WMI classes for specific system components.
