Home > PowerShell > Get computer uptime using Powershell

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 {

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”

}

No related content found.

  1. NC
    August 17, 2010 at 10:30 pm | #1

    Thx. This helped a lot.

    NC

  2. December 6, 2011 at 12:08 am | #2

    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”

    • December 6, 2011 at 1:23 pm | #3

      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”

  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>