This article came about because sometimes when you’re installing the software, If that software checks for the reboot, flag it could be rather irritating server apply updates on reboot.
Note : This article does not support never rebooting your server as that’s critical for many types of updates and patches, and it can lead to an unstable server, this is also at your own risk if you wish to install software with the pending reboot flag set.
They used to be a single location in the registry where you could check to see if the dependently flag was set, However, as we advance with operating systems, many of the new installers now check if multiple places, this therefore can be a pain to manually check all of these locations.
I have therefore created a script that checks these keys and updates them, which should remove the reboot, pending flag, and it also backs them up just in case you wished to restore them.
If you wish to restore them, you can simply run the registry file exported in the same directory where you run the script from.
Script : ClearBootFlag.ps1
# Requires -RunAsAdministrator
# Get the directory where the script is running
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$backupPath = Join-Path $scriptPath "PendingRebootBackup_$timestamp.reg"
$logPath = Join-Path $scriptPath "PendingRebootLog_$timestamp.txt"
# Initialize backup reg file with header
$regHeader = @"
Windows Registry Editor Version 5.00
"@
Set-Content -Path $backupPath -Value $regHeader
# Set console colors for better visibility
$host.UI.RawUI.BackgroundColor = "Black"
$host.UI.RawUI.ForegroundColor = "White"
Clear-Host
# Function to write colored output and log
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
$logMessage = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message"
Write-Host $logMessage -ForegroundColor $Color
Add-Content -Path $logPath -Value $logMessage
}
Write-ColorOutput "Starting pending reboot registry cleanup..." "Cyan"
Write-ColorOutput "Log file created at: $logPath" "Cyan"
# Array of registry keys and values to check
$rebootKeys = @(
@{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing"
Name = "RebootPending"
RegPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing"
},
@{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
Name = "RebootRequired"
RegPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
},
@{
Path = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager"
Name = "PendingFileRenameOperations"
RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager"
},
@{
Path = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager"
Name = "PendingFileRenameOperations2"
RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager"
}
)
# Check and process each registry key/value
foreach ($key in $rebootKeys) {
try {
$value = Get-ItemProperty -Path $key.Path -Name $key.Name -ErrorAction SilentlyContinue
if ($value -ne $null) {
Write-ColorOutput "Found pending reboot registry entry: $($key.Path)\$($key.Name)" "Yellow"
# Backup specific value before removal
$regKey = "[{0}]" -f $key.RegPath
$regValue = "`"{0}`"=" -f $key.Name
if ($value.$($key.Name) -is [string[]]) {
# Handle multi-string values
$regValue += "hex(7):"
$hexValues = @()
foreach ($str in $value.$($key.Name)) {
$hexValues += [System.Text.Encoding]::Unicode.GetBytes($str + "`0")
}
$regValue += ($hexValues | ForEach-Object { $_.ToString("X2") }) -join ","
$regValue += ",00,00"
}
elseif ($value.$($key.Name) -is [string]) {
# Handle string values
$regValue += "`"{0}`"" -f $value.$($key.Name)
}
else {
# Handle other types (DWORD, etc.)
$regValue += "dword:" + [Convert]::ToString($value.$($key.Name), 16)
}
Add-Content -Path $backupPath -Value ""
Add-Content -Path $backupPath -Value $regKey
Add-Content -Path $backupPath -Value $regValue
Write-ColorOutput "Backed up registry value: $($key.Path)\$($key.Name)" "Green"
# Remove the registry value
Remove-ItemProperty -Path $key.Path -Name $key.Name -Force -ErrorAction Stop
Write-ColorOutput "Successfully removed registry value: $($key.Path)\$($key.Name)" "Green"
}
else {
Write-ColorOutput "No pending reboot value found at: $($key.Path)\$($key.Name)" "Gray"
}
}
catch {
Write-ColorOutput "Error processing $($key.Path)\$($key.Name): $_" "Red"
}
}
# Check for and clear Component Based Servicing registry key
$CBSKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending"
if (Test-Path $CBSKeyPath) {
try {
Write-ColorOutput "Found Component Based Servicing pending packages key" "Yellow"
# Backup the PackagesPending key if it exists
$regKey = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending]"
Add-Content -Path $backupPath -Value ""
Add-Content -Path $backupPath -Value $regKey
# Export the values
$values = Get-ItemProperty -Path $CBSKeyPath
foreach ($value in $values.PSObject.Properties) {
if ($value.Name -notin @('PSPath', 'PSParentPath', 'PSChildName', 'PSProvider')) {
$regValue = "`"{0}`"=" -f $value.Name
if ($value.Value -is [string]) {
$regValue += "`"{0}`"" -f $value.Value
}
else {
$regValue += "dword:" + [Convert]::ToString($value.Value, 16)
}
Add-Content -Path $backupPath -Value $regValue
}
}
Remove-Item -Path $CBSKeyPath -Recurse -Force
Write-ColorOutput "Removed Component Based Servicing pending packages key" "Green"
}
catch {
Write-ColorOutput "Error removing CBS pending packages key: $_" "Red"
}
}
Write-ColorOutput "Registry cleanup completed" "Cyan"
# Display summary and instructions
$summary = @"
SUMMARY:
1. Registry backup file created at: $backupPath
2. Log file created at: $logPath
3. To restore the registry keys, double-click the backup file
WARNING:
While this script has cleared pending reboot registry entries, the system may still require
a reboot for proper stability. Use this script with caution and at your own risk.
"@
Write-ColorOutput $summary "Cyan"
Add-Content -Path $logPath -Value $summary
Then in the same folder you have registry file you can run to restore the values to previous values: