17

I would like to schedule a one-time server restart, e.g. to finish installing updates early in the morning. How can I do this from the command line on Windows 2012?

On Windows 2008, I would have used the at command,

at 2am shutdown -r -f -c "restart"

and taken the rest of the afternoon off.

But on Windows 2012, running this command tells me that

The AT command has been deprecated. Please use schtasks.exe instead.

So the equivalent command with schtasks.exe might be

schtasks /create /sc once /tn restart /tr "shutdown - r -f ""restart""" /st 02:00

Apart from being very forgettable, this command has another important downside: it schedules the task for 2am today—not much use unless I'm awake at 1am to run it.

According to the help for schtasks.exe, the /sd switch for setting the start date is not applicable with /sc once. So even if I wanted to type out tomorrow's date in mm/dd/yyyy format—and I don't—I can't do this.

The only possible solution I've found is here, where Kevin Traas suggests creating batch file to create a scheduled task just before midnight that waits for a couple of minutes and then creates another scheduled task to run the command that you actually want to run. Clever, but nowhere near as convenient as at.

  • @Lijo that link doesn't contain any information about how to schedule a restart. –  Jul 01 '17 at 23:37
  • This is just total stupidity to replace AT with unusable schtasks. Why they couldn't make both? Jerks! – midenok Dec 06 '18 at 00:11

8 Answers8

15

The shutdown command itself has a delay parameter /t that delays a shutdown for a number of seconds up to 10 years. If you want to schedule a shutdown in 14 hours, say, you might run

shutdown /r /t 50400

You might also add a reason with the /d parameter or a comment with /c; run shutdown /? for details.

Eamon Nerbonne
  • 316
  • 2
  • 5
8

Despite the documentation, the /SD parameter seems to be compatible with the /SC ONCE. The task is successfully created to run on the date provided, at the time provided. (Tested on W8 and W7)

Additionally, the XP documentation for schtasks.exe goes so far as to say the /SD parameter is required when using /SC ONCE, so I imagine there are a fair number of scripts using the combination.

Example:

C:\Users\Mitch>schtasks /create /sc once /tn restart /tr "shutdown -r -f ""restart""" /st 23:00 /sd 2013-06-13
SUCCESS: The scheduled task "restart" has successfully been created.

C:\Users\Mitch>

If going against the documentation does not sit well with you, consider generating the XML file directly (schema is here), which is definitely supported and definitely supports a task scheduled to run once on a future date. An easy way to get the proper file is to create it in the Task Scheduler mmc snap in and use the export command.

Example:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2013-06-12T21:20:51.004181</Date>
    <Author>FOOBAR\Mitch</Author>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2013-06-13T22:20:29</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>FOOBAR\Mitch</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>Your command here</Command>
    </Exec>
  </Actions>
</Task>

Command to import:

schtasks /CREATE /TN "Task Name" /XML filename.xml
Mitch
  • 2,343
  • 14
  • 22
  • Thanks for the tip about `/sd` being supported. That's interesting about the XML import too—although personally I don't care about going against the documentation. So at least this is possible—just much, much more difficult than it used to be with `at`. –  Jun 13 '13 at 03:25
  • Mildy interesting: `/sd` doesn't work with `/sc once` on Windows Server 2008. It results in `ERROR: Incorrect Start Date.` –  Jun 13 '13 at 07:06
  • 4
    Hmm... I additionally tested Server 2008 SP2 and Server 2008 R2 without issue. I will note that the date format is based off of your local date/time format, and leading zeroes are required. In the default EN-us install, this would lead to formatting like `/SD 06/14/2013`. – Mitch Jun 13 '13 at 20:59
3

I ended up creating runat.ps1 to replicate some of the simple features of at, incorporating the wisdom from Mitch's answer.

The syntax is runat (time) (command), e.g.

runat 2am shutdown -r -f -c "restarting the server"
runat 10:15 msg * it`'s 10:15 everyone!

If you, too, are nostalgic for the good old days when you didn't have to type...

schtasks /create /ru system /rl highest /sc once /tn restart /tr shutdown -r -f -c \"restarting the server\" /st 02:00 /sd 06/15/2013

... then you can install it by running this powershell command:

iex (new-object net.webclient).downloadstring('https://gist.github.com/lukesampson/5779205/raw/install.ps1');install runat https://gist.github.com/lukesampson/5778559/raw

Or you can manually download runat.ps1 script here.

1

I used this in a script for 2012 R2 and works nicely; It will create a daily task that restarts the server @ 4:30am

schtasks /create /ru system /rl highest /sc DAILY /tn restart /tr "shutdown /r /f /c "Restarting" /st 04:30

Please make sure that you run this from a CMD prompt with Administrative privileges.

Andy

1

Just another powershell script to programatically reboot the computer the next day at specified time

$tomorrowDate = (get-date).AddDays(1).ToString("dd/mm/yyyy")

schtasks /delete /tn restart /f
schtasks /create /sc once /ru MyDomain\administrator /rp /tn restart /tr "shutdown -r -f" /st 07:13 /sd $tomorrowDate
Marc Cals
  • 111
  • 3
0

I suggest using the schtasks command with the /f parameter.

Using /f, if there is a task with the same name, it's modified.

It's very useful for deploy tasks via scripting.

A full example of a schedule server restart:

schtasks /create /ru system /rl highest /sc DAILY /tn Reinicio /tr "shutdown /r /f /t 0 /c "Reinicio"" /st 22:30 /F

Extracted from: http://www.sysadmit.com/2016/09/windows-programar-apagado-o-reinicio-automatico.html

0

If the goal is simply to reboot after installing updates, then you can use the PSWindowsUpdate module to install the updates. You can download it from here. Once installed, just open Powershell and type

Get-WuInstall -AcceptAll -AutoReboot
Charles Burge
  • 758
  • 6
  • 16
0

This script lets you schedule the Restart of multiple servers at a specific date / time.

https://gallery.technet.microsoft.com/Schedule-restart-of-2c1131fb?redir=0