Overview
Intro
Here is a self contained snippet of code which I’ve assembled to easily check whether a reboot is pending on a given machine for any reason. The list of check locations is based on/captured from the xPendingReboot DSC resource. Full script is at the end.
Scripts
Initialize a container for the output
$RebootCheck = @{}
Check for pending Component Based Servicing and store the result
$ComponentBasedServicingKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name
$RebootCheck['ComponentBasedServicing'] = $ComponentBasedServicingKeys -Split "\\" -contains "RebootPending"
Check for a pending Windows Update reboot
$WindowsUpdateKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name
$RebootCheck['WindowsUpdate'] = $WindowsUpdateKeys -Split "\\" -contains "RebootRequired"
Check for pending file rename
$RebootCheck['PendingFileRename'] = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0
Check for pending Computer Rename
$ActiveComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
$PendingComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
$RebootCheck['PendingComputerRename'] = $ActiveComputerName -ne $PendingComputerName
Return Reboot Check Results
$RebootCheck
Complete Script
$RebootCheck = @{}
$ComponentBasedServicingKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name
$RebootCheck['ComponentBasedServicing'] = $ComponentBasedServicingKeys -Split "\\" -contains "RebootPending"
$WindowsUpdateKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name
$RebootCheck['WindowsUpdate'] = $WindowsUpdateKeys -Split "\\" -contains "RebootRequired"
$RebootCheck['PendingFileRename'] = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0
$ActiveComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
$PendingComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
$RebootCheck['PendingComputerRename'] = $ActiveComputerName -ne $PendingComputerName
$RebootCheck