How to force automatic restarts after finish installing updates (VBScript)

Introduction

This VBScript can help to force automatic restarts to finish installing important updates.

Scenarios

Whenever after updates, the server does not reboot immediately. However if we remote into the server we will receive a notice that you have to restart the computer in three days. If the restart does not occur in three days, the computer displays a 15-minute countdown and then automatically restarts. By default, this automatic restart is delayed if the computer is locked, and the countdown will begin the next time that you sign in to the computer.

This script can help to force automatic restarts to finish installing important updates.

Script

Step 1: Click Start, type cmd in the search box on the Start Menu, right-click the cmd.exe icon, and then click Run as administrator.
Step 2:  Run this VBScript with cscript in the Windows Console (type the name of the script at the command prompt)

Step 3: If the script runs successfully, you will see something as shown in the following figure.
 

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

VB Script
Edit|Remove
regKeyPath = "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\AlwaysAutoRebootAtScheduledTime" 
AutoRebootValue = objShell.RegRead(regKeyPath) 
If Err.Number = 0 Then 
    If AutoRebootValue = 1 Then 
          WScript.Echo "You have been enabled automatic Windows Update restarts." 
    Else 
          objShell.RegWrite "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\AlwaysAutoRebootAtScheduledTime","1","REG_DWORD" 
    End If 
Else   
    objShell.RegWrite "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\","" 
    objShell.RegWrite "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\","" 
    objShell.RegWrite "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\AlwaysAutoRebootAtScheduledTime","1","REG_DWORD" 
    WScript.Echo "Successfully enabled automatic Windows Update restarts." 
End If

Prerequisite

Windows 8
Windows Server 2012