Checking System Uptime

by Jan 22, 2013

In a previous tip we explained how you read date and time information from WMI and convert it into a regular date and time information. With this technique, you can then manipulate and calculate with the regular date and time functions. So, this piece of code returns the days a given system is running since the last reboot was done:

$os = Get-WmiObject -Class Win32_OperatingSystem
$boottime = [System.Management.ManagementDateTimeConverter]::ToDateTime($os.LastBootupTime)
$timedifference = New-TimeSpan -Start $boottime
$days = $timedifference.TotalDays
'Your system is running for {0:0.0} days.' -f $days

Twitter This Tip! ReTweet this Tip!