header image

PowerShell v6: #5 Get-Uptime

Posted by: | November 23, 2017 Comments Off on PowerShell v6: #5 Get-Uptime |

One new feature of PowerShell v6 (its actually been available since alpha 13 but I’d missed it) is the Get-Uptime cmdlet

 

PS C:\scripts> Get-Uptime

Days              : 0 
Hours             : 2 
Minutes           : 57 
Seconds           : 6 
Milliseconds      : 0 
Ticks             : 106260000000 
TotalDays         : 0.122986111111111 
TotalHours        : 2.95166666666667 
TotalMinutes      : 177.1 
TotalSeconds      : 10626 
TotalMilliseconds : 10626000

 

You get a timespan object returned for the uptime.

 

If you want to get something similar in Windows PowerShell you need to access the Win32_OperatingSystem CIM class

PS>  (Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem | select -ExpandProperty LastBootUpTime)

Days              : 0 
Hours             : 3 
Minutes           : 7 
Seconds           : 7 
Milliseconds      : 850 
Ticks             : 112278501750 
TotalDays         : 0.129951969618056 
TotalHours        : 3.11884727083333 
TotalMinutes      : 187.13083625 
TotalSeconds      : 11227.850175 
TotalMilliseconds : 11227850.175

 

Ah! We’re back to the days of the PowerShell one liner – though to be accurate it should one pipeliner.

 

Remember that Windows 8, 8.1 and 10 will only reset LastBootUpTime on a restart. If you  use Shutdown on the Start menu its effectively hibernating which explains why these versions of windows boot up so quickly.

under: PowerShell v6

Comments are closed.

Categories