0

Get Uptime and Last Reboot Status Using Powershell

If you’re on a single machine and want to check when the last time a computer rebooted, you would use the systeminfo command and search for the System Boot Time property of that command. But what if you wanted to check the status of multiple computers? What about wanting to check them remotely?. In this article I’ll share the Get-Uptime script that was initially written by Reddit user /u/OttoVonMonstertruck and added upon by yours truly. The whole idea is be to be able to get the last reboot time remotely as well as get-uptime for multiple computers in your org.

Powershell Uptime Script – Why is it needed?

There are many reasons for a systems administrator to want to know when the last time a computer or server was rebooted. One reason that comes to mind is how long it has gone without being patched. Normally, at least in the Microsoft world, when you apply Windows Updates it requires a system restart for those installed updates to be applied. So being able to run this one liner Powershell command would be able to tell you if it has been more than a month or not.

Aside from Windows updates, I’m sure we have all come across that one user who absolutely hates to restart their computer. They have like 50,000 tabs open along with 30 other windows open and wonder their system is always slow. I’ve come across this several times during my previous life as a desktop administrator and it seems to always be an issue when they have to “stop what they’re doing and reboot.” While there is some validity to it, there never seems to be a good time because of whatever reason they come up with.

I’m sure there are many more use cases to check the last reboot time in Windows Server but they can all be identified with this handy Powershell that I use pretty regularly.

Get Uptime and Last Reboot Status – Powershell Script

Function Get-Uptime {
<#
.Synopsis
    This will check how long the computer has been running and when was it last rebooted.
    For updated help and examples refer to -Online version.


.NOTES
    Name: Get-Uptime
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2018-Jun-16

.LINK
    https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/ -


.PARAMETER ComputerName
    By default it will check the local computer.


    .EXAMPLE
    Get-Uptime -ComputerName PAC-DC01, PAC-WIN1001

    Description:
    Check the computers PAC-DC01 and PAC-WIN1001 and see how long the systems have been running for.

#>

    [CmdletBinding()]
    Param (
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            Position=0)]

        [string[]]
            $ComputerName = $env:COMPUTERNAME
    )

    BEGIN {}

    PROCESS {
        Foreach ($Computer in $ComputerName) {
            $Computer = $Computer.ToUpper()
            Try {
                $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
                $Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)
                [PSCustomObject]@{
                    ComputerName  = $Computer
                    LastBoot      = $OS.ConvertToDateTime($OS.LastBootUpTime)
                    Uptime        = ([String]$Uptime.Days + " Days " + $Uptime.Hours + " Hours " + $Uptime.Minutes + " Minutes")
                }

            } catch {
                [PSCustomObject]@{
                    ComputerName  = $Computer
                    LastBoot      = "Unable to Connect"
                    Uptime        = $_.Exception.Message.Split('.')[0]
                }

            } finally {
                $null = $OS
                $null = $Uptime
            }
        }
    }

    END {}

}

We’ll first want to start by getting an array of the computers we want to check against. In my case I am going to run a Powershell command to get all the servers in my domain (that are not disabled) and save it to a variable. That portion looks like this:

#Get all enabled servers that are in the domain.
$Servers = Get-ADComputer -Filter {(OperatingSystem -like "*Server*") -and (Enabled -eq $true)} -Properties OperatingSystem | select -ExpandProperty Name | Sort-Object

Get All Enabled Servers in the Domain
 

Once we got that we’ll run the Get-Uptime script:

#Get all reboot times and put them in a variable to reference later.
$Reboots = Get-Uptime -ComputerName $Servers

#Sorting by Lastboot time.  
$Reboots | Sort-Object LastBoot

The results are sorted by LastBoot property and the output looks something like this.

Get-Uptime Script Results
 

How to run the Get Uptime script

In order to the run the script there are a couple of things you need to do.  First and foremost, you need to set your execution policy to RemoteSigned.  This is a standard with running any Powershell script.

Next you need to dot source the script since it is a function.  To dot source the script do the following:

  • Copy the script above and save it any location. In this example I’ll save it to my C:\_Scripts folder.
  • Within the Powershell Window type: . .\_Scripts\Get-Uptime.ps1 – Note the two dots before the backslash.

Dot-Source-Get-Uptime-Script
 

And that’s it for getting the Powershell last reboot status. Hopefully this is exactly what you were looking for. On a another note, be sure to check us out on our YouTube Channel for more awesome content. I’m sure you’ll also find some of our real world Powershell scripts very useful here in our Powershell Gallery

5/5 - (36 votes)

Paul

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.