Using Powershell Check For Pending Reboot and Last Restart on Remote Computer

We want to check for a pending restart. If a server is awaiting a reboot before attempting to to install new software or make OS changes. These powershell examples will help with checking the last reboot date and time as well as return a Yes and No if it is in need of a restart.

Note: Version 7.0+ now includes a commandlet called Get-Uptime that returns the “the time elapsed since the last boot of the operating system”. Here’s an example:

Get-Uptime
Get-Uptime -Since  ##last reboot

Get-wmiObject and Operating System Info

The get-wmiobject powershell commandlet provides information such as get, start or stop services as well as things like the OS and BIOS information on a remote machine. WMI stands for “Windows Management Instrumentation” and we can use it to find out when then server was last restarted.

In this example we are invoking the Wind32 Operating System class so we can ask the OS when the last time it was restarted.

Get-WmiObject -Class Win32_OperatingSystem -Computer "MyServer03"

If we encapsulate the command in parenthesis we can use dot notation to get the Last Boot Up Time from its properties. But there’s a catch…

(Get-WmiObject -Class Win32_OperatingSystem -Computer "MyServer02").LastBootUpTime

You will notice that the LastBootUpTime results of this powershell command will return something like “20210821015102.496682-240” that needs to be converted. Luckily we can assign the object to a variable and use a convert method to get the correct date format.

$wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer "MyServer02"
$wmi.ConvertToDateTime($wmi.LastBootUpTime)

Result: Saturday, August 21, 2021 1:51:02 AM

This results in a more readable format and a legit Datetime datatype to work with if needed.

Get-CimInstance vs Get-WmiObject

Looking into this I found another solution here that uses the Get-CimInstance that returns the correct Date and Time format automatically.

(Get-CimInstance Win32_OperatingSystem -ComputerName "MyServer01").LastBootUpTime

Check for Pending Restart

I found a simple function for checking that the server has a pending reboot status here and there’s a similar version here too. It uses the Invoke-Command statement to send a few “get” powershell commandlets to a remote server to grab the registry info.

So far I have test tested this on Windows 2012, 2016 and 2019 and it seems to work well whether the server has been patched or an application was installed and requires a reboot before installing something new.


## Does the Server require a restart ?
$servername = 'MyServer02'

Invoke-Command -ComputerName $servername -ScriptBlock { 
     if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return $true }
     if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return $true }
     if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true }
     try { 
       $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
       $status = $util.DetermineIfRebootPending()
       if(($status -ne $null) -and $status.RebootPending){
         return $true
       }
     }catch{}
 
     return $false

}