Search code examples
powershellinstallationautomationregistry

How can I check for a pending reboot?


I am trying to get to know where reboot is required or not for a Windows machine. However, my script is throwing and error.

powershell "$key = Get-Item "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue"

Error :
Get-Item : A positional parameter cannot be found that accepts argument
'Update\RebootRequired'.
At line:1 char:8
+ $key = Get-Item
HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Aut ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindin
   gException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.GetItemCommand

I am running this command in "command prompt". Not sure what it means !


Solution

  • You need to check 2 paths, one key and you need to query the configuration manager via WMI in order to check all possible locations.

    #Adapted from https://gist.github.com/altrive/5329377
    #Based on <http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542>
    function Test-PendingReboot {
        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
    }
    
    Test-PendingReboot