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:
HKLM\Software\Windows\CurrentVersion\Component Based Servicing
And the RebootRequired key under:
HKLM\Software\Windows\CurrentVersion\WindowsUpdate\Auto 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:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'
Or
Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
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:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'} 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:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'}
}
@{
Name = 'Reboot Required by Windows Update: '
Test = { Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'}
})
$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.
This is indeed a great post for me since I regularly use remote Pc for my official work.
Hi. Thanks. FYI the first lines of registry you mention is missing the word Microsoft. The Powershell commands are correct though. Dave
You helped me a lot, the only thing I added was a little script to take servers from a file, for mass