SEARCH

How-To Geek

Lesson 2: Using Task Scheduler to Run Processes Later

clip_image001

In this edition of Geek School, we’re going to teach you about the extremely powerful Task Scheduler utility that Windows uses behind the scenes to do all sorts of things.

You might be thinking that the Task Scheduler is just a way to schedule an application to run at a certain time, but it is so much more than that, and has become an integral part of Windows.

In the old days, Windows had a ton of Services running in the background at all times just to do things like trigger system cleanup events or maintenance that had to run at a particular time. Modern versions of Windows abandoned this practice as much as possible, and instead just added events to the Task Scheduler so that they would do the same thing, but without requiring a running process and wasting memory all of the time.

This doesn’t mean that Windows has no services running, of course, because many things need to stay running all the time in the background, both because they require constant activity and to communicate with other processes. But the migration of system tasks to the Task Scheduler has been a great thing for PC users.

Understanding the Task Scheduler Interface

When you first open the Task Scheduler application, you’ll see three panes of information: the left-hand pane with a list of folders in a tree format that is used to organize the list of scheduled tasks, the middle pane that displays the tasks themselves, and the Actions pane on the right that gives you quick access to common menu items.

Don’t be intimidated by all of the things in the interface – it is really quite simple and there aren’t as many options at the cluttered interface make it seem that there are.

clip_image002

The right-hand pane can actually be disabled using the right-most icon on the toolbar, as all of the actions are also available in the Action menu. If you have a task selected from the list, the menu will change to a task specific set of items like Run or End, but when you first open Task Scheduler or don’t have a task selected, you’ll see the options to create tasks, display running tasks, and all the rest.

clip_image003

We’ll explain how to create tasks in a minute, but first we should go through some of the options in the list and explain what they do.

  • Create Basic Task gives you a wizard interface for creating scheduled tasks.
  • Create Task uses the full detail view where you can manually create a task with any option you want.
  • Import Task lets you import tasks that you have previously exported. Very useful for copying a configuration to a secondary PC, or adding a task back after reinstalling.
  • Display All Running Tasks shows a list of all the tasks that are currently running as well as what folder you can find that task in.
  • Enable / Disable All Tasks History turns on detailed logging for everything that Task Scheduler does.
  • New Folder… creates a new folder in the left-hand pane, mostly useful for organizing a bunch of scheduled tasks, should you ever need to do that.
  • Delete Folder deletes the folder that you made and realize you didn’t need in the first place.
  • Refresh and Help should be self-explanatory.

The “Display All Running Tasks” option pops up a really simple list that shows which tasks are currently running, although it never seems to populate the Started or Run Duration columns. In any case, it’s handy to see if things are running.

clip_image004

The Enable / Disable All Tasks History option is really useful because it enables the History tab and tracks almost everything that happens. If you are troubleshooting a problem that involves a Scheduled Task, you should enable this option to get more data to work with – just make sure to disable it again afterwards, because that is a lot of unnecessary logging that might slightly degrade performance if you don’t need it.

clip_image005

We’ll go into great detail shortly about the details of the rest of the task detail tabs, but it is worth looking through many of the folders to see the types of things that are being run with a scheduled task by Windows or third-party software.

For instance, if you head down into the Microsoft -> Windows -> Defrag folder, you’ll see the ScheduledDefrag task in the list. The actions tab shows you what is actually being run behind the scenes, and you can check out what the rest of the options are set to.

Note: we wouldn’t recommend messing around with the options for built-in Microsoft services. Consider it informational only.

clip_image006

Another fun example is the Microsoft -> Windows -> ApplicationData folder, which has the CleanupTemporaryState task. Looking at the Actions tab tells us the actual command line, which uses the rundll32.exe component to run the Windows.Storage.ApplicationData.dll file, and calls the CleanupTemporaryState function within that DLL.

What does this mean? You can feel free to look it all up for yourself, but essentially all .NET applications provide the ability to use the ApplicationData functions to create temporary files or data that are only for the current session. So Windows calls the CleanupTemporaryState function every so often to clear out those temporary files, which are also located in the regular Windows Temp folders.

clip_image007

Since you are already wondering, you don’t need to manually trigger this task to keep your PC clean – if you want to clear out those temporary files, just use Disk Cleanup or something like CCleaner. But it’s fun to learn a little more about how Windows works under the hood.

Speaking of Disk Cleanup, did you know that Windows will automatically run it in silent mode whenever you are really low on disk space?

clip_image008

The command line for this scheduled task shows how it is done – by passing the /autoclean and /D %systemdrive% arguments to cleanmgr.exe, Windows tells Disk Cleanup to automatically run and to clean the drive that Windows is installed on.

clip_image009

You’ll notice with some of these built-in tasks that the Trigger might not be set. That could be for a variety of reasons, including that Windows might trigger the scheduled task a different way.

Creating Basic Tasks with the Wizard

Now that we’ve gone through some of the options in the interface, the only thing remaining is to understand how to create tasks.

The Basic Tasks wizard helps you quickly create a task without needing to understand everything about the interface. It’ll prompt you for a Trigger, which is what Windows uses to decide when to run the task. The trigger could be a time or date, whether daily, weekly, monthly, or just every other Tuesday at 1PM. The options are very powerful once you select one and head to the next screen.

You can also choose to launch a task when the computer starts, when you log on, or even when a specific event shows up in the Event Viewer, which we’ll be covering in the next lesson.

clip_image010

Once you get through the Trigger settings, you’ll be prompted for the Action that you want to perform, which is just a way of saying that you will use this screen to start a program. You can still select to send an email or display a message, but chances are good that the feature will be removed in a future version, since they both show up as deprecated now.

clip_image011

On the next screen you’ll be prompted for a program or script, and you can add arguments to that script as well. The last option for “Start in” is really useful if your application or script is looking for a file in the current folder, so you can set the current folder to where the file is.

clip_image013

That’s all there really is to it for creating a task using the wizard, but that is just the rookie way to do things.

Creating a Task with the Details View

Once you already know what you are doing, there’s no reason to go through a wizard to create a new task (unless you really like them). Instead, you can just create a new task and fill out the sections with the settings that you want – and naturally, there are more settings available this way.

Note: Everything we’re going to mention here also applies to editing existing tasks, so it’s well worth paying attention.

The first and most obvious fields are the Name, Location, Author, and Description, which are informational and self-explanatory. The Name field should be something short but descriptive, and you should really add a description so you don’t forget why you added this task when you are looking at it a year from now.

clip_image014

The rest of the options are better explained with a bulleted list, so we’ll use one of those now.

  • User account allows you to specify which user this task is going to run as. If you want the task to interact with the desktop, you’re going to probably want to use your own user account, which is the default. If you specify the SYSTEM account, you won’t want to save a password.
  • Run only when user is logged on – this option is the default, and it will automatically run using your user credentials in and in the current session. If you haven’t logged on, the task won’t run.
  • Run whether user is logged on or not – this option is the alternative to the last one, and it will run the task as your user account even if you are not logged in. This is useful for maintenance tasks that need to run even on a shared computer while somebody else is logged on, or if you regularly log out of your PC. You will be prompted to enter your password to create the task, and Windows will use those credentials to automatically log you in for the task.
  • Do not store password – If you don’t want your password stored, you can select this option – although it only works in a domain environment and requires the account be given the Logon as batch job permission. This is something you would use in a server environment where you didn’t want to save the passwords, so you use a service account. There are also restrictions on using Windows networking and encryption when using this option. (Summary: for the home user, don’t use this one).
  • Run with highest privileges – this option essentially gives the task administrator privileges the same way the User Account Control (UAC) prompt would do it. If you plan to use this for administrative purposes, you will probably want to enable this option.
  • Hidden – if you want the tasks to hide from the list by default, you can select this, although displaying of hidden tasks is enabled by default in the view options for Task Scheduler, so it seems fairly silly.
  • Configure For – this option should be set to whatever the latest operating system you are running is. Basically, you can create tasks that are meant to be exported and then run on older computers where Task Scheduler has less features. If you set the drop-down to XP, for instance, many of the options on the Triggers page would be missing, as well as in Conditions and Settings.

The Triggers page gives you a whole bunch of options to choose from, and even more advanced settings once you select one of them.

clip_image015

These options are fairly straightforward – when you choose an option from the list, the other settings on the page will adjust to give you more controls. These are the ones on the list:

  • On a schedule – this trigger uses a time and date setting to start the task. There are a ton of options in here, and you can also make a task recurring, which is probably the default usage.
  • At log on – only triggers when you first login. This could be useful to run a task when you login that requires administrator privileges – if you were to stick that into your Startup folder, it wouldn’t work the way you might expect. You can also use this to only trigger when a specific user logs in, rather than when every user logs in.
  • At startup – this triggers when the computer starts up, rather than after you login. If you wanted something to run on boot but before you login, this would be the way to go (although there isn’t a guarantee that it would run before you login, just that it would trigger earlier).
  • On Idle – this very useful trigger will only start when your computer is idle and you haven’t been in front of it for a while.
  • On an event – you can trigger a scheduled task whenever a particular event happens in the Event Viewer logs. So, for instance, every time that event 5133 shows up in the log, run a task that emails somebody.
  • At task creation/modification – essentially you are creating a task that runs automatically once the task is created. It’s a way for Windows to create a task on the fly to run something only under certain conditions, and then you can even use the option to delete the task automatically once it is complete. You probably won’t use this one much.
  • On connection / disconnect from user session – you can create tasks that only trigger when you logon or off via Remote Desktop, or when you switch between users on a local PC using fast user switching. This can be useful for running certain tools automatically when you connect to a server, for instance.
  • On workstation lock / unlock – this trigger starts a task whenever you lock the screen using WIN + L or unlock it with your password.

It’s worth noting that you can have multiple triggers defined for a single task.

Selecting the “On a schedule” option will give you more things to choose from, like which days of the week you’d like to run the task, and what time. You can choose to make it always happen in universal time instead of a time zone if you have servers that span the globe.

clip_image016

But selecting the “On an event” option, for instance, will give you more selectors to choose the log, source, and Event ID that you are filtering for. It’s all relative to what you choose.

clip_image017

The Advanced settings for Triggers give you a bunch of other options to choose from.

  • Delay task for up to (random delay) – this will add in a random delay so the task doesn’t always happen at the exact same time of the day.
  • Repeat task every – this will let you repeat the task more often than daily or weekly – if you wanted to have a task that ran every hour, you can use this option to make that happen.
  • Stop task if it runs longer than – this option is useful if your task has the potential to run away and never quit. Windows will automatically stop the process after the time specified.
  • Activate – this setting (not shown in the screenshot below) will set a time and date at which this trigger will be active. This is useful if you wanted to run something on idle, but not until after a particular date.
  • Expire – this allows you to set a task that only works for a month, or two, or whatever time you want to set it to. After the time period specified, the trigger would stop working (if you had multiple triggers, the task would continue to be active).
  • Enabled – You can use this to enable or disable a task.

clip_image018

The Actions page is much more self-explanatory… the only option is to create a new action that starts a program. You can technically still create an action that sends an email, but they are removing that ability, so if you need to send an email you’ll be better off with a script instead.

The Program/script box allows you to select the application you want to run, the Add arguments box allows you to pass an argument to that application, and the Start in option allows you to set the working directory.

For instance, if you were to use the Browse button and pick your web browser’s .exe file, you could then put http://www.howtogeek.com into the Add arguments box, and this scheduled task would automatically launch How-To Geek in your default browser when run. You could even set it up to run every morning to automatically open your browser when you login. (Not that we are specifically recommending this, but we’re just saying).

clip_image019

The Conditions tab has more options that work alongside the Trigger to decide whether or not the task will actually run. For instance, you could set a trigger to run every day at 1PM, but if you choose to “start the task only if the computer is on AC power”, it will only run if your laptop is plugged into the wall.

clip_image020

The options are pretty simple:

  • Start the task only if the computer is idle for – This will only start the task if the computer is idle for the number of minutes specified in the dropdown. Keep in mind these conditions are in addition to the trigger – so if you set a trigger for 1PM it won’t actually start until after 1PM and after the computer has been idle for x minutes.
  • Wait for idle for – this continues to wait for you to take a break and get off the computer for a set amount of time. This should be ideally set to less than what the trigger is set for, so it isn’t continuing to wait from the last time.
  • Stop if the computer ceases to be idle – There’s nothing more annoying than coming back to your PC just to find out that it is grinding its gears. This option will make the task stop itself when you come back to the PC.
  • Restart if the idle state resumes – This option will let the task start again if you walk away, assuming it didn’t finish the first time.
  • Start the task only if the computer is on AC power – you wouldn’t want to run a large backup task when you are running on batteries, and this option allows you to make sure that you are plugged in.
  • Stop if the computer switches to battery power – if you had a backup task running and then you unplugged the PC, you probably wouldn’t want that backup task continuing to be running and wasting your battery. This option lets you choose that option.
  • Wake the computer to run this task – most computers go into sleep mode these days, and this option allows the computer to automatically wake up and run the maintenance tasks. Very useful for backups at 2AM.
  • Start only if the following network connection is available – you can specify that a task runs only when connected to a particular network. For instance, you could specify that whenever you come home and plug into the wired network, a backup would automatically happen. Useful!

The last tab to understand is the Settings tab, which has a lot more options to choose from. The most interesting one is to allow the task to run on demand, which will let you trigger it any time you want.

clip_image021

The options include:

  • Allow task to be run on demand – Say you have a backup task set to run once a week, but you decide you want to run a backup right now. By having this option checked, you can run it whenever you want.
  • Run task as soon as possible after a scheduled start is missed – If the computer was turned off, it is possible that a triggered time event might not happen. This setting will allow Task Scheduler to go back and see that it was supposed to run, and then trigger it to run right away.
  • If the task fails, restart every – You probably won’t use this much, but if you want the task to restart itself automatically, you can use this option, as well as the Attempt to restart option to make sure it doesn’t go crazy and restart itself forever.
  • Stop the task if it runs longer than – If you have a task that goes out of control sometimes, you can use this to make sure it doesn’t run forever.
  • If the running task does not end when requested, force it to stop – this is useful if the tasks tend to completely hang, because it will force quit them.
  • If the task is not scheduled to run again, delete it after – this option will automatically delete the task after the last scheduled time. For instance, you could create a task that does something 5 times, and then self-destructs.
  • If the task is already running, then the following rule applies – one of the worst things with scheduled tasks is having the same task running 100 times because the previous instance didn’t close itself properly. This option allows you to make sure that that doesn’t happen by either not starting, running in parallel, queueing, or stopping the old one first.

Hopefully you’ve got a better understanding of how all these options work at this point. There are definitely a lot of options, and a lot of power at your fingertips. You just need to consider how to use it to your advantage.

Running a Scheduled Task on Demand via a Shortcut

We mentioned before that you can run a task on demand, and that is done using the command-line tool schtasks.exe and passing arguments over to specify which task you are trying to run.

You can also use the schtasks utility to create, delete, query, change, end, or display more information about a scheduled task. In fact, if you just type schtasks at the prompt, you’ll see a giant list of every scheduled task. If you use the /? argument instead, you’ll see the syntax.

clip_image022

The full syntax for using the /run argument, which is probably the most useful one, is this:

schtasks /run /TN “taskname”

You can then create a shortcut to run the scheduled task by pointing at the schtasks launcher utility.

What’s the point? Remember when we mentioned how you can use the “Run with highest privileges” option? You can actually create a scheduled task that runs as if the UAC prompt was already selected, and make a shortcut to run an administrator tool without requiring a UAC prompt.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 04/29/14