How to Get System Information with PowerShell

📜Part of the PowerShell Complete Guide, every PowerShell post on this site, grouped by the job you are doing.
The short answer

Use Get-CimInstance, not the old Get-WmiObject. Win32_OperatingSystem and Win32_ComputerSystem answer most of it.

When you pick up an unfamiliar server, the same handful of questions come first. This is the block I keep for answering all of them at once.

The Summary Block

PowerShell
$os = Get-CimInstance Win32_OperatingSystem
$cs = Get-CimInstance Win32_ComputerSystem

[pscustomobject]@{
    ComputerName = $env:COMPUTERNAME
    OS           = $os.Caption
    Version      = $os.Version
    Manufacturer = $cs.Manufacturer
    RAM_GB       = [math]::Round($cs.TotalPhysicalMemory / 1GB, 1)
    CPUs         = $cs.NumberOfLogicalProcessors
    LastBoot     = $os.LastBootUpTime
}

On my own laptop:

Get-CimInstance building a system summary: computer name, Windows 11, version, manufacturer, RAM, CPUs and last boot time

On my machine it returns this:

PowerShell system summary built from Get-CimInstance: computer name HPAI01, Windows 11 Home, version 10.0.26200, HP, 7.8 GB RAM, 8 CPUs and the last boot time

Uptime

Uptime is not a property, it is a subtraction, which is why people struggle to find it:

PowerShell
$os = Get-CimInstance Win32_OperatingSystem
$up = (Get-Date) - $os.LastBootUpTime
"Up for {0}d {1}h {2}m" -f $up.Days, $up.Hours, $up.Minutes

Disk Space

PowerShell
Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" |
    Select-Object DeviceID,
        @{N='SizeGB'; E={[math]::Round($_.Size / 1GB, 1)}},
        @{N='FreeGB'; E={[math]::Round($_.FreeSpace / 1GB, 1)}},
        @{N='Free%';  E={[math]::Round($_.FreeSpace / $_.Size * 100, 1)}} |
    Format-Table -AutoSize
Text
DeviceID SizeGB FreeGB Free%
-------- ------ ------ -----
C:        106.2   28.4  26.7
D:        369.1  281.8  76.3

DriveType=3 means fixed local disks, which keeps network drives and USB sticks out of the answer.

Serial Number and Model

PowerShell
Get-CimInstance Win32_BIOS | Select-Object SerialNumber, Manufacturer
Get-CimInstance Win32_ComputerSystem | Select-Object Model, Domain

Need something that is not in the block? Ask Claude which CIM class holds it, the graphics card or the installed updates for example, and for the line to add.

Running It Against Another Machine

Every Get-CimInstance call takes -ComputerName, so the same block works remotely with WinRM enabled:

PowerShell
Get-CimInstance Win32_OperatingSystem -ComputerName APPSRV01, APPSRV02 |
    Select-Object PSComputerName, Caption, LastBootUpTime
Use CIM, not WMI. Get-WmiObject still works in Windows PowerShell 5.1 but is deprecated and absent from PowerShell 7. Get-CimInstance is the replacement, uses WinRM rather than DCOM for remoting, and is friendlier through firewalls.

Related


If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *