How To Tell If A Remote Computer Needs Reboot

Knowing when to reboot your computer is obvious but how to tell if a remote computer needs a reboot? It could be quite useful to a sysadmin to know which server or workstation is pending for a reboot, whether to finish up a regular Windows Update or a new software installation.

There are a few registry keys scattered in the system tray to flag a pending reboot. Here are two of them:

The RebootPending key at:

HKLMSoftwareWindowsCurrentVersionComponent Based Servicing

And the RebootRequired key under:

HKLMSoftwareWindowsCurrentVersionWindowsUpdateAuto Update

If any of the keys exists in their respective location, a reboot is needed for that computer. And PowerShell is pretty capable of checking their existence with a single line like below:

Test-Path 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionComponent Based ServicingRebootPending'

Or

Test-Path 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionWindowsUpdateAuto UpdateRebootRequired'

Returning True means your local computer needs a reboot.

To execute the same cmdlet on a remote computer, you will need help from Invoke-Command, such as:

$command = {Test-Path 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionComponent Based ServicingRebootPending'}
Invoke-Command -computer ComputerName -ScriptBlock $command

Obviously, in order to have a successful Invoke-Command execution, you need PSRemoting/WinRM enabled on the remote computer.

Binding all together, here is the snippet that you can use to check and tell if a remote computer needs a reboot to finish up what it’s been doing.

$pendingReboot = @(
    @{
        Name = 'Reboot Pending Status: '
        Test = { Test-Path 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionComponent Based ServicingRebootPending'}
    }
    @{
        Name = 'Reboot Required by Windows Update: '
        Test = { Test-Path 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionWindowsUpdateAuto UpdateRebootRequired'}
    })
$computername = "ComputerName"
$session = New-PSSession -Computer $computername
foreach ($test in $pendingReboot) {
    $result = Invoke-Command -Session $session -ScriptBlock $test.Test
    $test.Name + $result
}

The result will look like this:

Lastly, thanks to 4sysops for sharing the idea. However, I had trouble using their code so I took a different route to get the same result.

Source