Most Efficient Way to Present System Uptime?

Tagged: 

This topic contains 12 replies, has 5 voices, and was last updated by Profile photo of Vern Anderson Vern Anderson 2 years, 4 months ago.

  • Author
    Posts
  • #17542
    Profile photo of Timothy Warner
    Timothy Warner
    Participant

    Hey everyone,

    Who remembers [b]systeminfo[/b]? I liked to use [b]systeminfo | more[/b] 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:

    [b]Uptime: 8 days, 12 hours, 36 minutes[/b]

    Thanks much! Glad to be a part of this community.

    Respectfully,
    Tim W.

    PS: I did use the forum search for "uptime" before submitting this topic. 🙂

  • #17544
    Profile photo of Don Jones
    Don Jones
    Keymaster

    If you mean *aggregate* uptime, probably nothing easy. If you mean *current uptime since last reboot,* it's query Win32_OperatingSystem using CIM, get the last boot time, and calculate (it's simple subtraction).

  • #17548
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    Hi Tim,
    Try this: (see the attached file for script code – some characters are mis-displayed here..)

    function Uptime {
        $OS = Get-WmiObject -class win32_operatingsystem
        $Uptime = (get-date) - ($os.ConvertToDateTime($os.lastbootuptime))
        $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes"
        Write-Output $Display 
    }
    

    usage would be like:

    uptime
    

    and output would look like:

    Uptime: 14 days, 21 hours, 45 minutes
    

    Output can be directed to a file as usual as in:

    uptime > myfile.txt
    

    It can be modified to show seconds and milliseconds as in:

    function Uptime {
        $OS = Get-WmiObject -class win32_operatingsystem
        $Uptime = (get-date) - ($os.ConvertToDateTime($os.lastbootuptime))
        $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes, " + $Uptime.Seconds + " seconds, " + $Uptime.Milliseconds + " milliseconds"
        Write-Output $Display 
    }
    
  • #17559
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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. http://letitknow.wordpress.com/2012/08/26/shutdown-and-fast-startup-in-windows-8/ is where I found some more details on that.

  • #17560
    Profile photo of Timothy Warner
    Timothy Warner
    Participant

    Sam, thanks a lot for that code. I've attached a copy of my tricked-out profile script; it may be useful to somebody!

    -Tim

  • #17583
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    Just returning the favor Tim, I recognized you right away. I took your 70-412 video course back in March of this year on CBT Nuggets when I was preparing for my MCSA 2012. It was most helpful..

    Dave, thanks for pointing that out! I, for one, did not know about this default behavior in Win8

  • #17587
    Profile photo of Timothy Warner
    Timothy Warner
    Participant

    Thanks for your kind words, Sam. I'm glad the 412 training was helpful to you. Like Don and Jeff Hicks, I'm training for Pluralsight now, FYI. -Tim

  • #17694
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    different ways to get computer uptime [url]http://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-to-show-85a7a4c9[/url] 🙂

  • #17698
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    New-TimeSpan -Seconds (Get-WmiObject Win32_PerfFormattedData_PerfOS_System).SystemUptime | Format-Table Days,Hours,Minutes

  • #17699
    Profile photo of Timothy Warner
    Timothy Warner
    Participant

    I'm having a great "time" with this! Thanks guys. -Tim

  • #17700
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    ([datetime]::Now – (New-TimeSpan -Seconds (Get-WmiObject Win32_PerfFormattedData_PerfOS_System).SystemUptime))

    # Displays the date and time of day the computer was started

  • #17701
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    Get-EventLog -LogName system | Where-Object { $_.eventID -eq 6005 -OR $_.eventID -eq 6006 -OR $_.eventID -eq 6008 }

    # Method using the event log instead

  • #17702
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    careful it looks like the forums injects a Unicode character in the 2nd one I posted where there should be an ASCII dash ( – )

You must be logged in to reply to this topic.

One Ping

  1. Pingback: