Get Last Computer Boot Time or Up Time With PowerShell

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.

Get-WmiObject -Class win32_operatingsystem -Property LastBootUpTime

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.

$os = Get-WmiObject -Class win32_operatingsystem
$os.ConvertToDateTime($os.LastBootUpTime)

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

$os = Get-WmiObject -Class win32_operatingsystem
$os.ConvertToDateTime($os.LastBootUpTime)
(get-date) - $os.ConvertToDateTime($os.LastBootUpTime)

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

$os = Get-WmiObject win32_operatingsystem
$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") 

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.

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

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.

Function Get-Uptime {
Param ( [string] $ComputerName = $env:COMPUTERNAME )
$os = Get-WmiObject win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue
 if ($os.LastBootUpTime) {
   $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" )
  }
  else {
    Write-Warning "Unable to connect to $computername"
  }
}

Get-Uptime -ComputerName RemoteComputerHostName

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

12 Responses

  1. Aladdin says:

    Awesome I like this website.

  2. orlando says:

    cool post, thanks for sharing it 🙂

  3. num says:

    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
    }

  4. Aravind Gosukonda says:

    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.

  5. Frank says:

    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?

  6. jake says:

    how do you get remote computers mapped drives in powershell?

    • Steve Parankewich says:

      You would like to have a remote computer map a drive? A mapped drive takes place on the user session which requires a login to that user session.

  7. Cornel says:

    Hi.
    You could limply Combine cmd command with powershell and have :
    systeminfo | where {$_ -match “Time:”}
    I find it much easier than multiple lines of code just to get the uptime.

Leave a Reply to Frank Cancel reply

Your email address will not be published. Required fields are marked *