Commandline Schtasks - Run once and Delete

Discussion in 'Windows Server' started by Jan K, Feb 25, 2005.

  1. Jan K

    Jan K Guest

    Hello there,

    I desperately need a way to create tasks via the commandline (bat file) that
    have the setting "Delete the task if it is not scheduled to run again"
    enabled (checked in the GUI).

    I have found no flag or switch in the documentation and helpfile for
    schtasks.exe. Is there any existing?

    Is there any other way to change the default setting so that all scheduled
    tasks are created with this setting checked (e.g. via a registry setting or

    A script is running and creating tasks up to 50 times per day, and the
    accumulation of the tasks that already has executed is not a desired feature
    that we want on a production server.

    Any help appreciated!
    Jan K, Feb 25, 2005
    1. Advertisements

  2. You can schedule the following batch to start when the computer starts.

    It then deletes all tasks that are scheduled to never start again.
    It wakes up every 15 minutes and checks again.

    @echo off
    @ping -n 901>nul
    for /f "Tokens=*" %%a in ('schtasks /Query^|FIND "Never"') do (
    set task=%%a
    set work=!task:*Never=!
    set work=!work: =!
    set task=!Task:Never=!##
    set task=!task: =!
    set task=!task: ##=!
    set task=!task:##=!
    if "!work!" EQU "" SCHTASKS /DELETE /F /TN "!task!"
    goto again

    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    Jerold Schulman, Feb 25, 2005
    1. Advertisements

  3. I post a more accurate solution at tip 9100 in the 'Tips & Tricks' at
    ( )

    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    Jerold Schulman, Feb 25, 2005
  4. Jan K

    Jan K Guest

    Hi Jerold,

    Thanks very much for the updated answer - that will definitely do the trick!

    (I'm sligthly disappointed thought that there's was no flag created, when
    the team developed the replacement for AT.EXE for setting the option to
    delete the task once it the task has been run - as it's within the GUI)

    Jan K, Feb 27, 2005
  5. Jan K

    lowspeed Guest

    You know its weird if you go to the GUI sched tasks there is an option to
    delete the task if it only runs one time.

    Shouldn't there be a switch for command line as well ?

    lowspeed, Feb 28, 2005
  6. Jan K

    Jan K Guest

    Hey Oren,

    Funnily enough, as a colleague of mine pointed out to me, there *IS* a
    switch for it, but here's the funny part: There seems to be different
    versions of SCHTASKS.EXE depending on which system you are running.

    For instance on my XP Professional system (fully patched, Service Pack II),
    the switch is non-existing and will result in an "Invalid Argument/Option"
    error, but on the Windows Server 2003 the switch is there (/z)!

    So basically, one could say that it's a case of RTFM but a slightly more
    sophisticated one!

    Jan K, Feb 28, 2005
  7. Jan K

    lowspeed Guest

    Wow great stuff !

    I'm running win2003 server.

    Thanks !
    lowspeed, Feb 28, 2005
  8. Jan K

    lowspeed Guest

    Just a little follow up question - OT.

    Where can i find the log errors associated with the schedtasks ?

    lowspeed, Feb 28, 2005
  9. Jan K

    Todd J Heron Guest

    In Control Panel open the Scheduled Tasks folder > on the Menu bar click
    Advanced > View Log.
    Todd J Heron, Feb 28, 2005
  10. Jan K

    lowspeed Guest


    The doc on microsoft site is very limited hence the next question:

    Anyone know if there's a number limit on the tasks scheduled ?

    lowspeed, Mar 1, 2005
  11. Jan K

    Todd J Heron Guest

    I have never seen or heard of a published limit.
    Todd J Heron, Mar 1, 2005
  12. Jan K

    Jan K Guest

    Just to clarify, the switch is /Z (not /z).

    Jan K, Mar 1, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.