In Windows Server 2008, scheduling a one-time server restart was straightforward with the at
command. For example:
at 2am shutdown -r -f -c "restart"
However, Windows Server 2012 deprecated the at
command in favor of schtasks.exe
, which offers more flexibility but comes with a steeper learning curve.
The direct equivalent using schtasks.exe
would be:
schtasks /create /sc once /tn restart /tr "shutdown -r -f \"restart\"" /st 02:00
This creates a task named "restart" that triggers at 2 AM. However, there's a significant limitation: it schedules for the current day only, making it impractical for next-day scheduling.
Since /sd
isn't compatible with /sc once
, we need an alternative approach. Here's a reliable method using PowerShell:
$TriggerTime = (Get-Date -Hour 2 -Minute 0 -Second 0).AddDays(1)
$Action = New-ScheduledTaskAction -Execute "shutdown.exe" -Argument "-r -f -t 0 -c ""Scheduled restart"""
$Trigger = New-ScheduledTaskTrigger -Once -At $TriggerTime
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "NextDayRestart"
This PowerShell script creates a task that will execute at 2 AM the following day.
For environments where PowerShell isn't available, this batch solution creates a two-step process:
@echo off
:: Create initial task to run just before midnight
schtasks /create /tn "MidnightPrep" /tr "%~dp0create_restart_task.bat" /sc once /st 23:59
:: Content of create_restart_task.bat
schtasks /create /tn "EarlyMorningRestart" /tr "shutdown -r -f -t 0" /sc once /st 02:00
The first task runs at 11:59 PM to create the actual restart task for 2 AM the next day.
To verify your scheduled tasks:
schtasks /query /tn "NextDayRestart"
To delete a task after it runs (add to your shutdown command):
shutdown -r -f -t 0 & schtasks /delete /tn "NextDayRestart" /f
For production servers, consider adding:
- Logging of shutdown events
- Email notifications
- Pre-shutdown checks for active users
Here's an enhanced PowerShell example:
$TriggerTime = (Get-Date -Hour 2 -Minute 0 -Second 0).AddDays(1)
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-Command \"Write-EventLog -LogName System -Source 'User32' -EventID 1074 -Message 'Scheduled restart initiated'; shutdown -r -f -t 60 -c 'Server restarting in 1 minute'\""
$Trigger = New-ScheduledTaskTrigger -Once -At $TriggerTime
$Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "ProdRestart" -Settings $Settings
In Windows Server 2008, scheduling a one-time server restart was straightforward using the at
command:
at 2am shutdown -r -f -c "restart"
However, Windows Server 2012 deprecated this command in favor of schtasks.exe
, which introduces some complications for scheduling future restarts.
The basic equivalent command using schtasks would be:
schtasks /create /sc once /tn "Server Restart" /tr "shutdown -r -f -c \"Scheduled restart\"" /st 02:00
This creates a task named "Server Restart" that triggers at 2:00 AM on the current day - which isn't useful if you're scheduling during business hours.
To schedule a restart for a specific future date, we need to use a different approach since /sd
isn't compatible with /sc once
. Here's a workaround:
schtasks /create /tn "Server Restart" /tr "shutdown -r -f -c \"Scheduled restart\"" /sc once /st 02:00 /sd 12/31/2023
Note that while this appears to work, the task might not execute properly due to the /sc once
limitation.
A more reliable method involves creating a task that runs just before midnight and schedules the actual restart:
@echo off
:: Create a batch file (schedule_restart.bat)
echo schtasks /create /tn "Final Restart" /tr "shutdown -r -f -c \"Final restart\"" /sc once /st 02:00 > C:\temp\restart_task.bat
:: Schedule the batch file to run at 11:55 PM
schtasks /create /tn "Prepare Restart" /tr "C:\temp\restart_task.bat" /sc once /st 23:55 /sd %date%
For more flexibility, consider using PowerShell:
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date "12/31/2023 02:00:00")
$Action = New-ScheduledTaskAction -Execute "shutdown.exe" -Argument "-r -f -t 0 -c \"Scheduled restart\""
Register-ScheduledTask -TaskName "Server Restart" -Action $Action -Trigger $Trigger
Always verify your scheduled tasks:
schtasks /query /tn "Server Restart" /v
Or in PowerShell:
Get-ScheduledTask -TaskName "Server Restart" | Get-ScheduledTaskInfo
- Test restart commands in a non-production environment first
- Ensure proper permissions when scheduling tasks
- Document scheduled restarts in your change management system
- Consider notifying users about planned maintenance