0

Get-Uptime and Last Reboot Status For Multiple Computers With 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 thme 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.

Get-Uptime and Last Reboot Time Powershell Script

Function Get-Uptime {

    [CmdletBinding()]

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

        [string[]] 
            $ComputerName = $env:COMPUTERNAME,
        
        [Switch]   
            $ShowOfflineComputers
    
        )

    BEGIN {
        $ErroredComputers = @()
    }

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

                $Object = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime

            } catch {
                if ($ShowOfflineComputers) {
                    $ErrorMessage = $Computer + " Error: " + $_.Exception.Message
                    $ErroredComputers += $ErrorMessage

                    $Properties = @{ComputerName  = $Computer
                                    LastBoot      = "Unable to Connect"
                                    Uptime        = "Error Shown Below"
                                    }

                    $Object = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime
                }
               
            } finally {
                Write-Output $Object        

                $Object       = $null
                $OS           = $null
                $Uptime       = $null
                $ErrorMessage = $null
                $Properties   = $null
            }
        }
    
        if ($ShowOfflineComputers) {
            Write-Output ""
            Write-Output "Errors for Computers not able to connect."
            Write-Output $ErroredComputers
        }
    }

    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 last time a comuter or server rebooted. 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.

Get-Uptime and Last Reboot Status For Multiple Computers With Powershell
5 (100%) 35 votes

Paul Contreras

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.