PowerTip: Get the Last Boot Time with PowerShell


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

 

 

Comments (22)

  1. mredwilson says:

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

  2. Anonymous says:

    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.

  3. Boe Prox says:

    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

  4. Geoff D. says:

    I always liked C:> wmic OS get LastBootupTime

  5. Ed Wilson says:

    @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

  6. tonyr says:

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

    thanks

  7. Eric says:

    Get-WmiObject -Class Win32_OperatingSystem -Property LastBootUpTime

  8. Ria Caussyn says:

    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…

  9. Dario Schiller says:

    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

  10. daye1997 says:

    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.

  11. Raju' says:

    just type systeminfo in cmd prompt

  12. Marcel P. says:

    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?

  13. NaveenS says:

    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.

  14. Josh Wieberg says:

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

  15. davide says:

    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

  16. ericnils says:

    I was looking for UpTime, not LastBootUpTime, so I wrote this function to get it for me. It also allows me to query a remote computer, even one that doesn’t have PowerShell as long as WMI is available. Unfortunately it doesn’t take advantage of CIM as
    I still need to support many non-CIM enabled computers. You could modify the function if your requirements are different.

    function Get-Uptime {
    Param(
    $ComputerName = $env:COMPUTERNAME
    )
    if ($lastBootUpTime = (Get-WmiObject win32_operatingsystem -ComputerName $ComputerName| select @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}).LastBootUpTime) {
    (Get-Date) – $lastBootUpTime
    } else {
    Write-Error "Unable to retrieve WMI Object win32_operatingsystem from $ComputerName"
    }
    }

    This returns a timespan object with the following properties (I like TotalDays):

    Days : 0
    Hours : 0
    Minutes : 28
    Seconds : 53
    Milliseconds : 760
    Ticks : 17337604162
    TotalDays : 0.0200666714837963
    TotalHours : 0.481600115611111
    TotalMinutes : 28.8960069366667
    TotalSeconds : 1733.7604162
    TotalMilliseconds : 1733760.4162

  17. OlKingKole says:

    Try this

    function Get-Uptime{
    Param(
    $ComputerName = $env:COMPUTERNAME
    )

    if($c=Get-WmiObject win32_operatingsystem -ComputerName $ComputerName){
    [datetime]::Now – $c.ConverttoDateTime($c.lastbootuptime)
    }else{
    Write-Error "Unable to retrieve WMI Object win32_operatingsystem from $ComputerName"
    }
    }
    "$(Get-Uptime)"

    ,Could Be

  18. test says:

    This is a test of formatting
    This should be indented

    and this is bold

  19. OleKingKole says:

    Try this


    function Get-Uptime{
    Param(
    $ComputerName = $env:COMPUTERNAME
    )

    if($c=Get-WmiObject win32_operatingsystem -ComputerName $ComputerName){
    [datetime]::Now - $c.ConverttoDateTime($c.lastbootuptime)
    }else{
    Write-Error "Unable to retrieve WMI Object win32_operatingsystem from $ComputerName"
    }
    }
    "$(Get-Uptime)"

  20. KalpeshS says:

    How can we use this Powershell command for different remote servers to get last boot time details.
    I want to get this details for all servers from one server. Please provide command(s) details.

  21. x says:

    Get-WmiObject win32_operatingsystem | select csname, @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}

  22. Brian says:

    This is sad. No command to simply get uptime?

    Being a Unix guy for 10+ years, and now new job which is majority windows. I have coined a new phrase.

    PATHETIC-SHELL

    There is nothing powerful about it, the syntax is wacked. Ain’t nobody got time to learn your verb-noun BS. Are we in elementary school?

    I’m installing perl on all my boxes when I get up tomorrow.

Skip to main content