Automating Python Scripts Using Windows Task Scheduler

Automating Python Scripts Using Windows Task Scheduler

Chances are, you’ve begun your journey using Python as a tool to automate some of your workflow. You have successfully turned your weekly reports that used to take you 3 hours to complete into a 30-second job with a click of a button. Your daunting weekly task has become such an insignificant part of your week that you almost forget to do it; you set a calendar reminder or alarm on your phone to remind you to run your Python script.

If this sounds like you, we can add another layer to your automation. Let the computer run it for you!

I’m going to walk you through a simple 3-step guide to automate the running of your Python script(s) using a program that comes on your computer by default, Windows Task Scheduler.

Step 1: Write Your Python Script

I know this seems like an obvious first step. If you are trying to automate a script, you probably have one already.

Mostly, the reason for this step in the walkthrough is to provide an example of what I’m doing. We’re going to be re-using the files we used in the GreenGeeks Cloud Automation, and As a reminder, their codes are below, respectively.

with open("itworks.txt", 'w') as file:
    file.write('hello world. This works')
from datetime import datetime

with open("itworks.txt", 'a') as file:
    file.write(f'\nThis file was updated at {}')

Take note of where you’re saving these files, since we’ll need that path. Mine are both located in E:\ShortAutomaton\example-scripts\WindowsTaskScheduler.

In fact, while we’re at it, go ahead and run which will create itworks.txt in the same folder. The rest of this will focus on

Step 2 (Optional): Write a Batch File That Runs Script

Windows Task Scheduler can run Python scripts as they are, but I’ve had some be a little finicky in the past, so I like to write a short batch file (file extension on those is .bat) which is basically a command you’d be writing in the command prompt or Powershell to achieve the same result. If you’re struggling to get your Python script to run in Task Scheduler, by the end of this article, come back here and try making a batch file.

For me, my Python folder is in the computer PATH. If you can run pip install in your command prompt without first locating your Python folder, you’re likely in the same boat, which makes it marginally easier. My example.bat file is one simple line.

python E:\ShortAutomaton\example-scripts\WindowsTaskScheduler\

Step 3: Create Task in Windows Task Scheduler

With the scripts ready, we can begin creating the task in Windows Task Scheduler that we’ll be scheduling. Start by going to your start menu and finding the Task Scheduler program. You can either search for it or you should find it in the Windows Administrative Tools folder in your Start menu.

Once it is open, you will find the Create Task… item on the right side of the window in the Actions section.

Windows Task Scheduler

Step 3.1: Give Your Task a Name & Description

You will start on the General tab. Add a name and description for your task. I chose Example Python Automation for my name and gave it a brief description that I’m using this as an example for this website.

Windows Task Scheduler General

Step 3.2: Set Task Triggers (We’re Using Time Schedule)

Next, you will go to the Triggers tab. This is where we will set the schedule for the task. Click the New… button at the bottom left.

A new window will pop up and will start with default settings of beginning the task On a schedule, One time, with at Start of today and the time is set to the time you opened the window.

In this example, let’s pretend we have a weekly report we are automating and we want to run this every Sunday at 9am.

Change the Settings to Weekly. When you click that, additional options appear on the right. We will recur every 1 week and check the box for Sunday. You’ll notice that there isn’t a place to select the time you want the script to run – just that it happens every Sunday. Go back up to where the Start is. You can change this time to 9am and that’s when it will run. This will even work if you set the start date to, say, Thursday. Keep in mind that we’re not saying when the script will start. We’re saying when the schedule starts.

There are advanced settings and I encourage you to explore them. I won’t get into them here.

Click OK and our trigger is set!

Windows Task Scheduler Trigger

Step 3.3: Set Actions

One more tab over to the Actions tab. This is where we’ll be selecting the script or batch file to run. Again, select New… at the bottom to pop open a new window.

Despite the drop down at the top, there’s really only one action here and it’s to run an application. Other actions are deprecated.

Setting Python Script

If we are running the Python script, the program/script we are running will be our Python install. You can quickly get this by opening your command prompt and running where python. The output will be the file path to your Python application.

The Start in will be the directory in which your script lives, in my case, E:\ShortAutomaton\example-scripts\WindowsTaskScheduler.

The Add arguments will be the file name of the script, in my case,

Windows Task Scheduler Action Python

Click OK and your action is set!

At this point, we’re done with the basic task creation, so you can click OK to create the task.

And that’s all there is to it! Your task is now set to run every Sunday at 9am.

If you don’t see your newly-created task in the Task Scheduler Library folder, hit Refresh on the right and it should populate in. You can also manually run it by selecting the task and clicking Run on the Action menu on the right.

Setting Batch File

Like I said, running the script directly has been finnicky for me in the past, so I like to run a batch file like what we created in Step 2, so we’ll walk through that as well.

You can do this step in place of the Setting Python Script step. Since I already have this task created, we’ll edit this. I’m doing it this way to explain that there is no Edit option on the task. You can edit it by selecting the task and clicking Properties.

Back on the Actions tab, we’ll edit the action we just created.

This time, we’re going to browse for our example.bat file. Select that and it will populate the entire path. For me, it will be E:\ShortAutomaton\example-scripts\WindowsTaskScheduler\

We also need to keep the Start in folder as E:\ShortAutomaton\example-scripts\WindowsTaskScheduler since we are using a relative reference in our script. (Note, you could also write the batch file in a way that changes the directory and you don’t have to deal with that in the Task Scheduler.)

Windows Task Scheduler Action Batch

Click OK and your action is set!

Click OK again to create (or save the edits to) your task!

Bonus: A Few Additional Options

There are a couple options I want to point out that can help you with running this task on a schedule with fewer errors. Both of the options I’ll be covering are on the General tab of the main task properties window under Security options.

The first is a radio button that is, by default, selected on “Run only when user is logged on”. You can select the option below it, “Run whether user is logged on or not”. This is a great option for a work computer if you lock your computer to go to meetings or the bathroom, etc. (which for security purposes, you should be locking your computer when you’re away from it). I had a task that I ran Monday mornings at 9:30am, but I was often pulled into meetings around that time. The task still executes.

The second is a checkbox just below that, “Run with highest privileges”. This is great for if you are running into problems where your schedule is rejected because you didn’t have privileges to run the command. It should be rare, but it can happen. I’ve found that it pairs well with running the task when the user is logged off.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top