Get-Uptime
Here’s a simple function to get a “readable” value of the uptime for the local computer (by default, but there’s a ‘Server’ parameter), and the last boot date/time.
function Get-Uptime { Param( [ValidateScript({Test-Connection -Quiet -ComputerName $_ })] $Server=$env:computername ) $wmi=Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server $lastBoot=$wmi.ConvertToDateTime($wmi.LastBootUpTime) $up=$wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime) $d=$up.days $h=$up.hours $m=$up.Minutes $s=$up.Seconds Write-Host "Uptime: $d Days $h Hours $m Min $s Sec" Write-Host "Last boot: $lastBoot" }
Usage:
# Get the local computer uptime and last boot time Get-Uptime # Get a remote computer uptime and last boot time Get-Uptime -Server xxx.xxx.xxx.xxx