none
Run a task once at midnight using schtasks

    Question

  • I would like to run a task in the next 24 hours.  Specifically, I use this technique to remind myself of appointments the following day, but let's just say that I want to run a task at midnight.  I am using Windows 7.

    I created a script that takes two arguments (hour and minute), and

    schtasks /create /SC ONCE /TN REMIND%1%2 /TR %HOME%\bin\reminder.bat /ST %1:%2

    Unfortunately, if I run this with "00 00" (midnight), then schtasks returns "Task may not run because /ST is earlier than current time.".  And indeed, it doesn't run.

    This used to be trivial with the "at" command.  Is it possible with the schtasks command, without having to manually put in tomorrow's date?  Also, is it possible to add the "/z" flag (I've seen some posts that suggest "/sc once" and "/z" conflict.

    Related URLs with info:

    http://social.technet.microsoft.com/Forums/en-NZ/w7itprogeneral/thread/ec43e6be-8d28-4527-842c-4664ae56697c

    http://support.microsoft.com/kb/313565

    http://technet.microsoft.com/en-us/library/cc725744%28v=ws.10%29.aspx#BKMK_once

    Tuesday, April 17, 2012 9:18 PM

Answers

All replies

  • Hi,

    To schedule a task that runs every day

    The following example schedules the MyApp program to run once a day, every day, at 8:00 A.M. until December 31, 2002. Because it omits the /mo parameter, the default interval of 1 is used to run the command every day.

    In this example, because the local computer system is set to the English (United Kingdom) option in Regional and Language Options in Control Panel, the format for the end date is DD/MM/YYYY (31/12/2002) 

    schtasks /create /tn "My App" /tr c:\apps\myapp.exe /sc daily /st 08:00 /ed 31/12/2002

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support


    Wednesday, April 18, 2012 2:08 AM
    Moderator
  • You are getting confused with the military time (or 24 hours clock): 00:00 and a 12 hour clock time: 12:00am

    So your parameter should be 12:00am 

    http://en.wikipedia.org/wiki/Midnight

    http://en.wikipedia.org/wiki/12-hour_clock#Confusion_at_noon_and_midnight


    Cyreli

    Wednesday, April 18, 2012 7:12 AM
  • I don't want to run the task daily, I want to run it once.
    Wednesday, April 18, 2012 5:33 PM
  • I don't believe that I'm getting confused by military time (I understand military time), but it was just an example anyways.  Let's say instead that it's currently 3PM and I want to set a reminder for 9AM the next day.  How would I do that?

    Wednesday, April 18, 2012 5:35 PM
  • Create a scheduled task to run once with the date and time you had in mind, and have it run your script. Seeing the scheduled task runs when you want it to, why does your script take two parameters for time?

    Also, this seems like a general question, that has nothing to do with Windows PowerShell

    Wednesday, April 18, 2012 7:46 PM
  • 1) I'm looking to use "schtasks" like I used "at", to create a reminder in the next 24 hours.  Yes, I could add the date, but then I'd have to figure out what tomorrow's date is, etc.  This is why in my initial post I wrote, "This used to be trivial with the "at" command.  Is it possible with the schtasks command, without having to manually put in tomorrow's date?" 

    2) As for why I pass two arguments, it's because you have to name the task that you're creating.  (Again, a change from the "at" command.)  In order to guarentee a unique name, I have decided to name my task "remindHHMM", and I run it at HH:MM.  I found the easiest way to create both of these values was to just pass two args.

    3) I wasn't sure what "schtasks" fell under.  I looked at other questions about schtasks, and it appeared that people placed it under PowerShell.  If you can suggest a specific forum to place this under, I'm happy repeating my question over there.

    Thanks.

    Wednesday, April 18, 2012 10:05 PM
  • You may want to look at http://technet.microsoft.com/en-us/library/cc725744(v=ws.10).aspx to see if schtasks can perform what you want. I never really used the command line for scheduling tasks, so I am not 100% sure on that part of it.

    The best place for this woould probably be the general forums, I don't have the permissions to move this thread, but I am sur esomeone can, so you don't have to re-create it. This would be a good place to post if your scheduled task ran a powershell script which was failing for some reason.

    Thursday, April 19, 2012 12:49 PM
  • It wasn' t that hard to post a new thread in the general forums.  Plus I could (hopefully) address in my initial post some of the default solutions people have tried posting above.  New thread is at:

    http://social.technet.microsoft.com/Forums/en-US/winservergen/thread/20b5a1f8-54e0-4615-9234-70da66be3384

    Tuesday, April 24, 2012 3:50 PM