How to use Task Scheduler to execute scripts

For Windows System Administrators automatization can become a real necessity in making their life much easier. Creating scripts and scheduled tasks are two important aspects to consider in this mechanism. There are many scripting languages that can be used to create scripts in Windows, but among the best suited are: Batch, Powershell or Visual Basic. I’ve used Powershell as the primary scripting language because it is a powerful and modular scripting language. I’ve said it’s modular because you can add/import CMDlets. Microsoft also released some Powershell updates (I think the newest version is 3.0) that increased it’s content a lot. Also note that Powershell is build on .NET Framework so you can create your own CMDlets. A script is nothing more than a set of commands written to execute a sequence of actions. In Powershell, the file extension is .ps1, in Visual Basic is .vbs or .vbe (I think there are some other accepted extensions) and of course, in Batch is .bat, .cmd or .btm.

   After you create the script, the next step is to determine the working conditions of the file. Think of the moment of the day/week/month in which the script will be executed. There can be some special conditions for running the script, maybe you want to execute it whenever you reboot the machine or you just want to run it once, manually. Both server and desktop editions come with the “Task Scheduler” tool. With this utility you can create repetitive tasks that are automatically executed by the machine once the conditions are met. After creating such task, the human interaction is not needed no more (where is the case). You can create tasks to execute scripts in a certain period of time. To open this tool navigate to “Administrative tools”, write Task Scheduler in the search box or type “taskschd.msc” from the run prompt. This is how Task Scheduler looks like:
Task Scheduler
   You can create tasks in the folders that are already created by the OS or you can create your own. Before showing you how to create a simple repetitive task make sure that you have a script file. I will create a script named test.ps1. Now, right click on the desired location and press “Create Task”. The same thing can be achieved by pressing the “Create Basic Task” button on the “Actions” menu in the right section. The difference between a “Basic Task” and a “Task” is that the second one has multiple features that can be configured:
Creating a task
Click the first tab and then enter the following:
– the “Name that will be displayed for this task
– a short “Description” that will explain in few words what is the purpose of the task
– the “username” under which privileges the task will run (I’ve deleted the domain names from the image).
– where necessary, check the “Run with highest privileges” box
Task Scheduler
After completing all these fields, click on the “Triggers” tab. Here is where we specify the conditions that will trigger the task. Now press the “New” button and the following window will appear:
Task trigger
You can set the task to be executed following a schedule, upon startup, in case of an event and many others. Check each running condition:
Task Scheduler conditions
I will select “On a schedule”, click on the “Daily” radio button and set the repeating interval to one day. On the “Advanced settings” section you can enable whatever option suits your needs. I will check the “Stop task if it runs longer than” button and set it to “3 days”. In the end, don’t forget to check the “Enabled” button:
Task Scheduler triggers
The newly created trigger will appear in the “Triggers” section. You can add multiple triggers, edit or delete them:
Task Scheduler trigger
After completing this step, click on the “Actions” tab. Here is where we specify what event will be triggered.  Now click the “New” button:
Task Scheduler actions menu
Because we want to run a Powershell script, we’ll have to select the “Start a program” option. In the “Program/script” section enter the path to the Powershell executable, in my case is C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe. Now, on the “Add arguments” section enter -command “& ‘C:\test.ps1’ ” where C:\test.ps1 is the path to your script. This step is very important so write it just like that. I lot of mistakes are made because the & character followed by a space is not entered:
Task Scheduler actions
The action was added and can be viewed on the “Actions” tab. You can add multiple actions, edit or delete them:
Task Scheduler actions menu
Next, let’s specify the “Conditions” that will determine if the task will run. Both triggers and conditions must be fulfilled to execute the task.
Task Scheduler conditions
On the “Conditions” tab “Power” and “Networking” options can be enabled and also how the computer should react in case of an idle period. Remember that if one condition is not true, the task will not execute. I will just enable the two “Power” settings.
Lastly, on the “Settings” tab, we can specify additional settings that will affect this task:
Task Scheduler conditions
Enable what option do you want and then click the “OK” button. The task has been created and it will be added in the specified folder. A new tab will appear on the task called “History”. Here is where all the task’s actions are recorded, everything from task registration to failed executions.:
Task Scheduler
   There is a tool available in command prompt called “Schtasks.exe” with which you can create, modify or delete scheduled tasks. Type Schtasks.exe /? to view all options for this utility:
Schtasks command
I’ll not show you how to use this tool because frankly I haven’t used it a lot. You can achieve the same things using the GUI. If you are really interested in this tool just search on Google.
   This is all folks for this post, hope you’ve enjoyed it. Stay tuned for the following articles, meanwhile enjoy IT training day and don’t forget to relax and have a great day.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s