How to set a reboot pending flag in the registry

There are multiple registry flags that can be used to tell the system a reboot is pending, but in this post, I'm going to cover my most favorite way.

Why set a reboot flag?

Have you ever ran a cleanup script that you know you need to reboot the system afterwards but don't want to immediately reboot? Maybe you've used SCCM, Intune, or some other RMM tool to clean up a workstation or server in the background but you need to wait until a maintenance window or for the user to finish working. This is just one way to handle that process. If you're adding this code snippet into a larger script, be sure it goes in at the bottom and you've properly handled errors; ideally returning or exiting out before you reach this stage so the flag can double as the script completely successfully!

The code to set the flag:

Run using an elevated PowerShell prompt...

New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Name "DVDRebootSignal" | Out-Null

What's so great about it?

Once you've set the flag, it will stay there until the system reboots. After the reboot, Windows will automatically remove this flag so you don't have to worry about it. It's a well-known location that's supported by almost every script or application to determine if there's a pending reboot. You can monitor for a pending restart and once the flag is cleared, you can proceed to the next step.