PowerTip: Get the Last Boot Time with PowerShell

PowerTip: Get the Last Boot Time with PowerShell

  • Comments 15
  • Likes

Summary: Learn how to get the last boot time for your computer.

Hey, Scripting Guy! Question How can I find the last boot time for my computer by using Windows PowerShell?

Hey, Scripting Guy! Answer In Windows PowerShell 3.0, use the Get-CimInstance cmdlet, and select the LastBootUptime property from the Win32_Operatingsystem WMI class:

PS C:\> Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime

csname                                     lastbootuptime

------                                     --------------

EDLT                                       3/22/2013 11:27:01 AM

Hey, Scripting Guy! Answer In Windows PowerShell 2.0 and Windows PowerShell 1.0, use the Get-WmiObject cmdlet, and then translate the returned date to a readable format:

PS C:\> Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'

;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}

csname                                     LastBootUpTime

------                                     --------------

EDLT                                       3/22/2013 11:27:01 AM

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I always liked C:\> wmic OS get LastBootupTime

  • @Geoff D. WMIC uses WMI, and in fact uses the same WMI class, Win32_OperatingSystem that I use in my examples here. The advantage of using the Get-CimInstance cmdlet in PowerShell 3 is that the time displayed is easily readable. For example, WMIC returns a time like this:

    LastBootUpTime

    20130328080040.485462-240

    The Get-CimInstance returns a time like this:

    lastbootuptime

    --------------

    3/28/2013 8:00:40 AM

  • is the primary advantage of using get-ciminstance vs gwmi is that the output is more suitable for human reading?  Just curious.

    thanks

  • @TonyR no, the primary advantage is that with remote connections it runs on WinRM instead of DCOM like traditional WMI does. This means, that it only requires one opening in the Windows Firewall, AND that it uses the same remoting that Windows PowerShell remoting uses. So if I can use CIM remotely, I can also use Windows PowerShell remoting. This is a huge deal, and the new CIM cmdlets offer dramatic advantages.

  • Get-WmiObject -Class Win32_OperatingSystem -Property LastBootUpTime

  • Why couldn't microsoft just done a lastuptime command?  Why do you have to write scripts for things that should be simple commands.  While I like the cmdlet functionality of powershell, I absolutely hate having to write scripts for things that should be simple commands.  I wonder if microsoft did this on purpose to drive people away from command line and back to gui...

  • Pretty sure it doesn't return the real boot time.

    From testing it, it looks like LastBootUpTime gives you the uptime since the last boot, and then substracts it from the current date and time.

    If you boot your PC at 8AM and do a check before lunch at 12AM it returns 8AM as Boot time.

    Now you suspend to disk, and wake the pc up at 1PM after lunch. If you check your boottiem now, it says 9AM, because the uptime is still 4hours

  • How do I have the Powershell prompts me for different remote server name (s) so that I can check remote server's LastBootUpTime, I need to continue to check one after one until I press CTRL-D to terminate it.

    Thanks.

  • just type systeminfo in cmd prompt

  • If you go the systeminfo route, you may as well go all in and turn it into a usable object. systeminfo /FO CSV | ConvertFrom-CSV

  • I've been looking into this, but unfortunatly it seems the value at Lastbootuptime is NOT correct. I have a system that has been in suspended mode for 2 weeks now. I do get the correct boottime when executing 'Net Statistics Server' at the command prompt. However, according to the LastBootUptime-propertry, this system has only been running for about 6 hours. What I need in my script is the REAL boottime & date. Where can I find (in WMI?) the value that I get when looking at the statistics of the Server-service? Anyone?

  • This work perfect but my question is is there any way to get those computer names which are not went in the pipeline, I mean to say those computer which are not accessible or RCP error or Access denied.?
    I would like to know if any server is shut down or deleted.

  • This work perfect but my question is is there any way to get those computer names which are not went in the pipeline, I mean to say those computer which are not accessible or RCP error or Access denied.?
    I would like to know if any server is shut down or deleted.

  • @Marcel P.
    I think he is correct. Net Statistics Server gives the accurate boot time.

  • I use this command from cmdlet:

    C:\>net statistics workstation

    and it shows in the second output line the time and date when the system booted up... like this:

    Workstation Statistics for \\COMPUTERNAME


    Statistics since 27/08/2014 01:12:42


    Bytes received 727627925
    Server Message Blocks (SMBs) received 25432
    Bytes transmitted 8928662