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

Introduction

This script 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

Step1: Start the PowerShell Console with administrator. To run the script in the Windows PowerShell Console, type the one command< Script Path> at the Windows PowerShell Console.

For example, type C:\Script\ForceAutomaticRestarts.ps1

The step is shown in the following figure.
 

When the script finishes running, Windows PowerShell Console displays brief information about the script.
 

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.

PowerShell
Edit|Remove
If(Test-Path -Path "$RegKey\WindowsUpdate\AU"{ 
    $AutoRebootValue = (Get-ItemProperty -Path "$RegKey\WindowsUpdate\AU").AlwaysAutoRebootAtScheduledTime 
  
    Switch ($AutoRebootValue) 
    {0{Set-ItemProperty -Path "$RegKey\WindowsUpdate\AU" -Name AlwaysAutoRebootAtScheduledTime ` 
               -Value 1 | Out-Null; Write-Host "Successfully enabled automatic Windows Update restarts."break} 
  
        $null {New-ItemProperty -Path "$RegKey\WindowsUpdate\AU" -Name AlwaysAutoRebootAtScheduleTime ` 
               -Value 1 -PropertyType DWord | Out-Null; Write-Host "Successfully enabled automatic Windows Update restarts."break} 
         
        Default {Write-Host "You have been enabled automatic Windows Update restarts."}}} 
Else 
{ 
     New-Item -Path "$RegKey\WindowsUpdate" -Name AU | Out-Null 
     New-ItemProperty -Path "$RegKey\WindowsUpdate\AU" -Name AlwaysAutoRebootAtScheduledTime ` 
     -Value 1 -PropertyType Dword | Out-Null 
  
     Write-Host "Successfully enabled automatic Windows Update restarts."}

Prerequisite

Windows PowerShell 2.0
Windows 8
Windows Server 2012