Scheduled Task for Reboot in Windows

article #1484, updated 15 days ago

The code below, deletes any past scheduled tasks of the name it uses, then sets up a new scheduled task for 3AM next, where it will run:

shutdown -f -r -t -0

and it does it using the SYSTEM account, whether Windows considers SYSTEM logged in or not, with the highest privileges.

# Delete old scheduled task of this name, if present

Unregister-ScheduledTask -TaskName "schtasks_REBOOT" -Confirm:$false 2> $null

# Create new scheduled task of this name

$action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\shutdown.exe' -Argument '-f -r -t 0'
$today = Get-Date
$tomorrow3AM = Get-Date -Day ($ + 1) -Hour 3 -Minute 0 -Second 0
$trigger = New-ScheduledTaskTrigger -Once -At $tomorrow3AM
$taskprincipal = New-ScheduledTaskPrincipal -UserID 'NT AUTHORITY\SYSTEM' -RunLevel Highest
Register-ScheduledTask -TaskName 'schtasks_REBOOT' -Action $action -Trigger $trigger -Description 'Scheduled Reboot' -Principal $taskprincipal