Archive

Posts Tagged ‘Uptime function in 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”

}