Skip to content

Instantly share code, notes, and snippets.

@lipkau lipkau/Get-Uptime.ps1

Last active Sep 5, 2015
What would you like to do?
Get Computer's uptime
function Get-Uptime
Get Computer's uptime
Returns the total time since the last reboot
AUTHOR : Oliver Lipkau <>
VERSION: 1.0.0 - OL - Initial Code
.PARAMETER ComputerName
Hostname/IP of the remote computer
.PARAMETER Credential
Authentication Credentials for the remote computer
[OutputType([TimeSpan] )]
[string]$ComputerName = "localhost",
begin { }
process {
if ($Credential)
$time = Get-WmiObject -class Win32_OperatingSystem -computer $ComputerName -Credential $Credential
$time = Get-WmiObject -class Win32_OperatingSystem -computer $ComputerName
$t = $time.ConvertToDateTime($time.Lastbootuptime)
New-TimeSpan $t $(get-date)
end { }
$upTime = Get-Uptime
"$($upTime.days)d $($upTime.hours)h $($upTime.minutes)m $($upTime.seconds)S"

This comment has been minimized.

Copy link
Owner Author

lipkau commented Sep 5, 2015

Output for "$($upTime.days)d $($upTime.hours)h $($upTime.minutes)m $($upTime.seconds)S"

1d 23h 3m 20S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.