# Most Efficient Way to Present System Uptime?

**URL:** <https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891>\
**Category:** PowerShell Help\
**Created:** [July 28, 2014, 11:43am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891 "2014-07-28T11:43:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![timothy-warner](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/timothy-warner/32/840_2.png) [@timothy-warner](https://forums.powershell.org/u/timothy-warner)\
**Post date:** [July 28, 2014, 11:43am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/1 "2014-07-28T11:43:43Z")

</div>

Hey everyone,

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

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. 🙂

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [July 28, 2014, 1:10pm UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/2 "2014-07-28T13:10:31Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [July 28, 2014, 8:17pm UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/3 "2014-07-28T20:17:00Z")

</div>

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 
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 29, 2014, 12:21am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/4 "2014-07-29T00:21:48Z")

</div>

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. [Shutdown and Fast startup in Windows 8 – Let IT know](http://letitknow.wordpress.com/2012/08/26/shutdown-and-fast-startup-in-windows-8/) is where I found some more details on that.

---

<div class="post-metadata">

**Author:** ![timothy-warner](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/timothy-warner/32/840_2.png) [@timothy-warner](https://forums.powershell.org/u/timothy-warner)\
**Post date:** [July 29, 2014, 12:41am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/5 "2014-07-29T00:41:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [July 29, 2014, 5:14am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/6 "2014-07-29T05:14:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timothy-warner](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/timothy-warner/32/840_2.png) [@timothy-warner](https://forums.powershell.org/u/timothy-warner)\
**Post date:** [July 29, 2014, 7:14am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/7 "2014-07-29T07:14:42Z")

</div>

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](http://pluralsight.org) now, FYI. -Tim

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [August 1, 2014, 1:08am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/8 "2014-08-01T01:08:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vern\_anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vern_anderson/32/5140_2.png) [@vern\_anderson](https://forums.powershell.org/u/vern_anderson)\
**Post date:** [August 1, 2014, 3:04am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/9 "2014-08-01T03:04:14Z")

</div>

New-TimeSpan -Seconds (Get-WmiObject Win32\_PerfFormattedData\_PerfOS\_System).SystemUptime | Format-Table Days,Hours,Minutes

---

<div class="post-metadata">

**Author:** ![timothy-warner](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/timothy-warner/32/840_2.png) [@timothy-warner](https://forums.powershell.org/u/timothy-warner)\
**Post date:** [August 1, 2014, 3:05am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/10 "2014-08-01T03:05:16Z")

</div>

I’m having a great “time” with this! Thanks guys. -Tim

---

<div class="post-metadata">

**Author:** ![vern\_anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vern_anderson/32/5140_2.png) [@vern\_anderson](https://forums.powershell.org/u/vern_anderson)\
**Post date:** [August 1, 2014, 3:08am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/11 "2014-08-01T03:08:01Z")

</div>

([datetime]::Now - (New-TimeSpan -Seconds (Get-WmiObject Win32\_PerfFormattedData\_PerfOS\_System).SystemUptime))

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

---

<div class="post-metadata">

**Author:** ![vern\_anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vern_anderson/32/5140_2.png) [@vern\_anderson](https://forums.powershell.org/u/vern_anderson)\
**Post date:** [August 1, 2014, 3:09am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/12 "2014-08-01T03:09:24Z")

</div>

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

# Method using the event log instead

---

<div class="post-metadata">

**Author:** ![vern\_anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vern_anderson/32/5140_2.png) [@vern\_anderson](https://forums.powershell.org/u/vern_anderson)\
**Post date:** [August 1, 2014, 3:11am UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/13 "2014-08-01T03:11:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/most-efficient-way-to-present-system-uptime/2891/14 "2024-05-16T20:47:43Z")

</div>


