All about time


timeIn a post in the Powershell Q&A forum, Tim Warner wrote:

Who remembers systeminfo? I liked to use systeminfo | more on my Windows XP and Win 7 systems to get the boot datetime, but Microsoft appears to have stripped the property in Windows 8 and Windows Server 2012. At any rate, it’s not what I want. :)

What would you say is the most efficient way in Windows PowerShell v3 or later to get not the boot time but the elapsed uptime of the box in an eyeball-friendly format?

Ultimately, I’d like to add this function to my profile script so I see the info at-a-glance every time I start the console. I’d like to see output that is similar to the following:

Uptime: 8 days, 12 hours, 36 minutes

I thought sure that should be easy via querying WMI from Powershell. Then Dave Wyatt  added:

Windows 8 will throw you all off, though. By default, when you “Shut down” a Windows 8 computer, you’re actually going into a sleep / hibernate state, and waking up from that doesn’t reset the WMI LastBootUpTime value (but a reboot does; go figure.) You can figure out the last “boot” time on Windows 8 by searching the event log, though.

Which got me thinking, with Windows 8 / Server 2012 and above we may not be getting uptime by querying WMI, so I looked into different ways to get computer uptime and wrote this script which can be downloaded from the Microsoft Script Center Repository.

Script output looks like:

time2

This script shows different ways to get and display computer uptime.  The script saves output to a log file in the folder where it runs. Log file name can be modified by editing the $LogFile variable.

The script gets uptime from:

  1. The VM’s Hyper-V host if the computer is a virtual machine
  2. Systeminfo
  3. Net statistics
  4. Event Log
  5. Win32_OperatingSystem class
  6. Win32_PerfFormattedData_PerfOS_System class (pointed out by Vern Anderson)

8/1/2014 – V.10

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s