Get Last Computer Boot Time or Up Time With PowerShell

Windows

You may require logic to take place around last boot time or computer up time. Or perhaps you may simply want to display it to the user. PowerShell provides an easy way to accomplish this with the Get-WMIObject commandlet. I will show you how to get both the last boot time as well as the current up time for a computer. I will also illustrate using a function to determine any computer’s up time remotely.

Step #1: The first step is to get the required WMI Object Class and property for the last boot-up time.

Step #2: The second step is to assign the object a variable and then convert the property of LastBootUpTime to a Date and Time object.

Step #3: The third step is to determine the up time of the computer.

Step #4: We can now combine the last boot up time and total up time to be viewed from the console

Step #5: The following function is a function created from our above script that can be executed to retrieve the current computer’s boot times.

Step #6: The following function is a function slightly modified to accept the ComputerName parameter than can be executed to retrieve the current computer’s boot time or a different computer’s boot time by specifying the host name.

Feel free to leave a comment or any requests you may have for PowerShell solutions.

Print Friendly, PDF & Email

7 comments

  • Nice article. I’v d maid this script, gatting uptime from AD:

    $servers = Get-ADComputer -Filter * | Select -ExpandProperty Name
    $servers | foreach {
    $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_
    $comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $_
    $se = Gwmi Win32_Bios -ComputerName $_ | select SerialNumber
    New-Object -TypeName PSObject -Property @{
    Name = $comp.Name
    username = $comp.username
    #Name = $os.hostname
    serial = $se.SerialNumber
    LastReboot = $os.ConvertToDateTime($os.LastBootUpTime)
    #$t = Get-Date $os.ConvertToDateTime($os.LastBootUpTime) -format g
    #Model = $comp.Model
    } #| Export-CSV -NoType C:\uptime\uptime.csv -Encoding Unicode -Delimiter “;”
    $s = $comp.Name + “;” + $comp.username + “;” + $os.ConvertToDateTime($os.LastBootUpTime)
    $s >> C:\uptime\uptime.csv
    }

  • Hello Steve,
    cool blog you got going on. Thanks for sharing the info and spreading the powershell word 🙂

    One point I’d like to comment on in this script is that instead of using Get-Date to calculate the uptime, you could use the LocalDateTime property of Win32_OperatingSystem class to avoid any miscalculation due to timezone differences.

  • PS C:\> Function Get-Uptime {
    $os = Get-WmiObject win32_operatingsystem -ErrorAction SilentlyContinue
    $uptime = (Get-Date) – $os.ConvertToDateTime($os.LastBootUpTime)
    Write-Output (“Last boot: ” + $os.ConvertToDateTime($os.LastBootUpTime) )
    Write-Output (“Uptime : ” + $uptime.Days + ” Days ” + $uptime.Hours + ” Hours ” + $uptime.Minutes + ” Minutes” )
    }

    Get-Uptime
    Last boot: 01/13/2016 07:56:02
    Uptime : 12 Days 2 Hours 24 Minutes

    I ran that and got the results shown. Interesting enough, I shutdown on Friday the 22nd, powered up at home on the 22nd and then shutdown on the 24th and powered up this morning. Any reason why it shows the uptime is 12 days and last boot was on the 13th?

Leave a Reply