Running this as admin in powershell will create a ps1 script that cancels pending restarts (with logging), and schedule it as a task.
Microsoft, don't be Apple. This also is not an effective way to get me to PAY for pro, if that's your game.
Create the script to cancel pending restarts with logging
$scriptContent = @"
`$logPath = "C:\Scripts\CancelRestartLog.txt"
function Log-Message {
param([string]`$message)
`$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"`$timestamp - `$message" | Out-File -FilePath `$logPath -Append
}
Log-Message "Script execution started"
$restartCancelled = $false
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) {
shutdown /a
`$restartCancelled = `$true
}
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) {
shutdown /a
`$restartCancelled = `$true
}
if (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) {
shutdown /a
`$restartCancelled = `$true
}
if (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) {
shutdown /a
`$restartCancelled = `$true
}
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) {
shutdown /a
`$restartCancelled = `$true
}
if (`$restartCancelled) {
Log-Message "Restart cancelled successfully"
} else {
Log-Message "No pending restart detected"
}
Log-Message "Script execution completed"
"@
Save the script to a file
$scriptPath = "C:\Scripts\CancelRestart.ps1"
New-Item -ItemType Directory -Force -Path (Split-Path $scriptPath)
Set-Content -Path $scriptPath -Value $scriptContent
Create a scheduled task
$taskAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -File "$scriptPath""
$taskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15)
$taskSettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 5)
$taskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName "CancelWindowsRestart" -Action $taskAction -Trigger $taskTrigger -Settings $taskSettings -Principal $taskPrincipal -Force
Write-Host "Script created and scheduled task set up successfully. Logs will be written to C:\Scripts\CancelRestartLog.txt"