≡ Menu

Get computer uptime using Powershell

Simple Way to get local computer uptime….

PS C:> (gwmi win32_operatingSystem).lastbootuptime
20100728180723.375199+330
PS C:>

Now, let’s query the remote computers uptime.

PS C:> (gwmi win32_operatingSystem -computer remotePC).lastbootuptime
20100728180723.375199+330
PS C:>

The above are oneliners, if you want to convert this into full fledged function, use the below code. This gives the uptime of computer you have parsed.

C:>Get-PCuptime -computer remotePC

System(remotePC) is Uptime since :  14 days 20 hours 54 minutes 20 seconds

C:>

Code for function:

function Get-PCUptime {            
[cmdletbinding()]            
param($computer)            

$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime            
$sysuptime = (Get-Date)  [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)            
Write-Host“System($computer) is Uptime since : ” $sysuptime.days “days” $sysuptime.hours `
“hours” $sysuptime.minutes “minutes” $sysuptime.seconds “seconds”            

}
{ 6 comments… add one }
  • NC August 17, 2010, 10:30 pm

    Thx. This helped a lot.

    NC

  • Ixobelle December 6, 2011, 12:08 am

    One liner from hell 😉

    Write-Host “System has been up for” ((Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)).Days “Days” ((Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)).Hours “Hrs” ((Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)).minutes “Min”

    • Sitaram Pamarthi December 6, 2011, 1:23 pm

      Ixobelle, You can make the one liner a bit smaller.

      Write-Host “System has been up for” ((Get-Date) – ([WMI]””).ConverttoDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)).Days “Days” ((Get-Date) – ([WMI]””).ConverttoDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)).Hours “Hrs” ((Get-Date) – ([WMI]””).ConverttoDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)).minutes “Min”

  • Gary August 22, 2012, 3:35 am

    I know this post is kind of old but I found it pretty useful. I wanted to share my update to it for error handling so it doesnt throw out a big scary red message if the computer is not reachable.

    function Get-PCUptime {

    param($computer)

    Trap {write-host “Cannot Connect to $computer” -Foregroundcolor Red;
    continue}

    $lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer -ea “SilentlyContinue”).LastBootUpTime

    $sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)

    if ($lastboottime -ne $NULL){
    Write-Host “System($computer) is Uptime since : ” $sysuptime.days “days” $sysuptime.hours `
    “hours” $sysuptime.minutes “minutes” $sysuptime.seconds “seconds”
    }
    }

Cancel reply

Leave a Comment