Auditing Server Uptime
When investigating a mysterious server crash or trying to verify if an automated patch-management system successfully rebooted a machine over the weekend, you need to know the exact timestamp of the last system boot. While you can open the Task Manager GUI and look at the “Up time” counter (which displays Days:Hours:Minutes), doing the reverse math in your head to figure out the exact date and time is frustrating.
Furthermore, if you are managing a cluster of 50 Hyper-V virtual machines, you need a way to script this query across the network. PowerShell allows you to query the Windows Management Instrumentation (WMI) database to extract the absolute, exact timestamp of the last boot.
Using the Win32_OperatingSystem Class
The data you need is stored in the LastBootUpTime property of the WMI Operating System class.
Open a PowerShell prompt and execute the following command:
Get-WmiObject Win32_OperatingSystem | Select-Object LastBootUpTime
The output, however, will look like complete gibberish:
LastBootUpTime
--------------
20240115083000.000000-300
This is the raw WMI datetime format (Year, Month, Day, Hour, Minute, Second). It is accurate, but not human-readable.
Formatting the Timestamp (PowerShell 5.1)
To convert this raw data into a standard, readable date format, you must use PowerShell’s management class converter method.
$OS = Get-WmiObject Win32_OperatingSystem
$OS.ConvertToDateTime($OS.LastBootUpTime)
The output will now display perfectly:
Monday, January 15, 2024 8:30:00 AM
The Modern Method (Get-CimInstance)
If you are using modern PowerShell syntax, the older Get-WmiObject cmdlet has been superseded by Get-CimInstance. The newer CIM cmdlets automatically handle the datetime conversion for you, making the code significantly shorter.
Run this single command:
(Get-CimInstance Win32_OperatingSystem).LastBootUpTime
It will instantly output the perfectly formatted date and time.
Querying Remote Servers
To check the boot time of a remote server (for example, a database server named SQL-SRV-01) without logging into it via Remote Desktop, simply append the -ComputerName parameter to the CIM command.
(Get-CimInstance Win32_OperatingSystem -ComputerName "SQL-SRV-01").LastBootUpTime
This allows you to rapidly audit the uptime of your entire infrastructure directly from your IT workstation.