Skip to main content

How to Check for Pending Reboot Status in Batch Script

In an automated environment, one of the most critical pre-flight checks a script can perform is to determine if the system is in a "pending reboot" state. Kicking off a long software installation, a critical backup, or a system configuration change on a machine that is about to reboot for a Windows Update can cause the task to fail unexpectedly. A robust script should be able to detect this state and either abort or wait.

This guide will teach you how to check for the most common indicators of a pending reboot. Since there is no single command for this, you will learn how to query the specific Registry keys where Windows components flag their need for a restart, using the built-in REG QUERY command.

The Challenge: Why is a Reboot Pending?

A pending reboot is not a single flag set in one place. Several different Windows components can independently request a restart after they perform an operation. The most common sources are:

  • Windows Update: After installing updates, the system is often left in a state where a reboot is required.
  • Software Installations: Many installers (MSI packages) require a reboot to replace files that were in use.
  • System Maintenance: Component-Based Servicing (CBS) or other low-level system changes can also flag a required reboot.

A reliable script must check the indicators for all these common sources.

The Core Method: Querying the Registry with REG QUERY

The REG QUERY command is the standard way to read values from the Windows Registry. We can use it to check for the existence of specific keys or values that signal a pending reboot.

The key to our script's logic is the ERRORLEVEL that REG QUERY returns:

  • ERRORLEVEL 0: Success. The key or value was found.
  • ERRORLEVEL 1: Failure. The key or value was not found.

Therefore, if we query a "reboot pending" key and get an ERRORLEVEL of 0, we know a reboot is needed.

The Key Registry Locations to Check

These are the three most important and commonly checked locations in the registry.

  1. Windows Update: The Windows Update agent creates this key when a restart is required. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired

  2. Component-Based Servicing: For reboots triggered by servicing operations (like DISM). HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending

  3. Pending File Renames: This value holds a list of files to be renamed/moved on the next boot, a classic indicator of a post-installation reboot. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager (Value: PendingFileRenameOperations)

The Script: A Comprehensive Pending Reboot Checker

This script checks all three locations and sets a single "flag" variable if any of them indicate that a reboot is pending. This script must be run as an Administrator.

@ECHO OFF
SETLOCAL
SET "RebootIsPending=0"

ECHO --- Checking for Pending Reboot Status ---
ECHO.

REM --- 1. Check Windows Update ---
ECHO Checking Windows Update...
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" /f ".*" > NUL 2> NUL
IF %ERRORLEVEL% EQU 0 (
ECHO -> Windows Update requires a reboot.
SET "RebootIsPending=1"
)

REM --- 2. Check Component-Based Servicing ---
ECHO Checking Component-Based Servicing...
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" > NUL 2> NUL
IF %ERRORLEVEL% EQU 0 (
ECHO -> Component-Based Servicing requires a reboot.
SET "RebootIsPending=1"
)

REM --- 3. Check for Pending File Rename Operations ---
ECHO Checking for pending file renames...
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager" /v "PendingFileRenameOperations" > NUL 2> NUL
IF %ERRORLEVEL% EQU 0 (
ECHO -> Pending file rename operations detected.
SET "RebootIsPending=1"
)

ECHO.
ECHO --- Summary ---
IF %RebootIsPending% EQU 1 (
ECHO [WARNING] A pending reboot is detected. It is recommended to restart this machine.
) ELSE (
ECHO [SUCCESS] No pending reboot was detected.
)

ENDLOCAL

How the Script Works

  • SET "RebootIsPending=0": We start with a flag variable set to 0 (false).
  • REG QUERY ... > NUL 2> NUL: For each check, we run REG QUERY. We are not interested in the command's output, only its ERRORLEVEL, so we redirect both standard output (> NUL) and standard error (2> NUL) to the "null" device.
  • IF %ERRORLEVEL% EQU 0 ...: After each query, we check the exit code. If it's 0, the key was found, which means a reboot is pending.
  • SET "RebootIsPending=1": If a reboot is detected, we set our flag to 1 (true).
  • Final Summary: At the end, we check the final state of our RebootIsPending flag to give a single, clear summary.

Common Pitfalls and How to Solve Them

The single biggest pitfall is permissions. The registry keys we are checking are in HKEY_LOCAL_MACHINE (HKLM), which is a protected part of the registry.

Example of an error: if run as a standard user, REG QUERY will fail with an "Access is denied" error, but because we are redirecting errors to NUL, the script might fail silently and give an incorrect "No pending reboot" result.

The Solution: Run as Administrator

This is non-negotiable. Any script that reliably checks for a pending reboot must be run with administrative privileges.

Practical Example: A "Go/No-Go" Pre-Task Script

This script demonstrates how to use the pending reboot check as a "gate" to prevent a critical task from running on an unstable system.

@ECHO OFF
SETLOCAL
SET "RebootIsPending=0"

REM --- Perform the checks (condensed for this example) ---
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" > NUL 2> NUL & IF !ERRORLEVEL! EQU 0 SET "RebootIsPending=1"
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" > NUL 2> NUL & IF !ERRORLEVEL! EQU 0 SET "RebootIsPending=1"
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager" /v "PendingFileRenameOperations" > NUL 2> NUL & IF !ERRORLEVEL! EQU 0 SET "RebootIsPending=1"

REM --- The Go/No-Go Gate ---
IF %RebootIsPending% EQU 1 (
ECHO [CRITICAL] A pending reboot was detected. Aborting the main task.
ECHO Please reboot the server before running this script again.
GOTO :End
)

ECHO [SUCCESS] System is stable. Proceeding with the main task...
ECHO.
ECHO Running critical application deployment...
REM (Your main application logic would go here)
TIMEOUT /T 5

:End
ECHO Script finished.
ENDLOCAL

Conclusion

Checking for a pending reboot is an essential part of writing robust, enterprise-grade automation scripts. While there is no single command to get this status, a simple and reliable check can be built using REG QUERY.

For a reliable check:

  • You must query the three key registry locations: Windows Update, Component-Based Servicing, and Session Manager.
  • The logic is based on the ERRORLEVEL from REG QUERY: 0 means a reboot-pending key was found.
  • Your script must be run as an Administrator to have the necessary permissions to read the registry.

By incorporating this check into your scripts, you can prevent many common failures and ensure your automation runs only when the system is in a stable state.