Run Your Python Scripts Automatically with Windows Task Scheduler


Hello again, tech enthusiasts! ๐Ÿ‘‹

You wrote a great Python script. It cleans a report, or saves attachments, or builds a summary. But you still have to remember to run it. If you are on holiday, or busy in a meeting, the script just waits.

Today we will make Windows run the script for you, every day, at the same time. We will use Task Scheduler. It is a program that comes with Windows. Think of it like an alarm clock: when the time comes, it does not ring, it starts your program.

Many people try this and the task “runs” but nothing happens. In this post, we will avoid the usual mistakes with three simple tricks: the full path to Python, the “Start in” folder and a log file.

What you need

  • Windows 10 or 11
  • Python 3 from https://www.python.org
  • A script, for example C:\Scripts\daily_report.py
  • 15 minutes

Step 1: Find the full path to python.exe

Task Scheduler does not always know where Python is. So we give it the full address. Open the Command Prompt and type:

where python

You will see something like C:\Users\YourName\AppData\Local\Programs\Python\Python312\python.exe. Copy this path. We will need it soon.

If you see two paths, use the one that you normally use (the one where you installed your libraries with pip).

Step 2: Add logging to your script

When a task runs in the background, you cannot see any messages. A log file is like a diary for your script. It writes down what happened and when. If something goes wrong at 6 in the morning, you can read the diary later.

Add this to the top of your script:

import logging
from pathlib import Path

# Save the log next to the script, not in a random folder
LOG_FILE = Path(__file__).parent / "daily_report.log"

logging.basicConfig(
    filename=LOG_FILE,
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
)

def main():
    logging.info("Script started")
    # ... your real work goes here ...
    logging.info("Script finished")

if __name__ == "__main__":
    try:
        main()
    except Exception:
        logging.exception("Script failed")  # writes the full error to the log
        raise

Path(__file__).parent means “the folder where this script lives”. This keeps the log in a place you can find.

Step 3: Create a .bat wrapper

A .bat file (batch file) is a small text file with Windows commands. Double-click it, and Windows runs the commands one by one. We use it as a wrapper: a small box around our script that sets everything up first.

Open Notepad and paste this. Change the paths to match your computer:

@echo off
REM Move to the script folder, so relative file names work
cd /d "C:\Scripts"

REM Run the script with the full path to python.exe
REM Any extra messages and errors go to run_output.log
"C:\Users\YourName\AppData\Local\Programs\Python\Python312\python.exe" "C:\Scripts\daily_report.py" >> "C:\Scripts\run_output.log" 2>&1

Save it as C:\Scripts\run_daily_report.bat. In Notepad, choose Save as type: All Files, or Windows may save it as .txt.

2>&1 means “send errors to the same file as normal messages”. So even a crash before logging starts will be recorded.

Double-click the .bat file to test it. Then open the log files and check that you see “Script started” and “Script finished”.

Step 4: Create the task

  1. Press the Windows key, type Task Scheduler and open it.
  2. On the right, click Create Basic Task.
  3. Give it a name, like Daily Report, and click Next.
  4. Choose Daily, pick a time (for example 07:00) and click Next.
  5. Choose Start a program and click Next.
  6. In Program/script, choose your run_daily_report.bat file.
  7. In Start in (optional), type C:\Scripts (no quotes here).
  8. Click Next, then Finish.

The Start in box is very important. Without it, Windows starts in a system folder, and your script cannot find files like sales.xlsx.

Step 5: Test the task now

Do not wait until tomorrow. In Task Scheduler, click Task Scheduler Library, find Daily Report, right-click it and choose Run.

Press F5 to refresh. Look at the Last Run Result column. The operation completed successfully. (0x0) means all is good. Then check your log files. ๐ŸŽ‰

Common problems

  • Last Run Result is 0x1. โ†’ Your script had an error. Open run_output.log and daily_report.log to see the message.
  • “No module named pandas”. โ†’ The task uses a different Python. Check the path from where python and use the one where you installed your libraries.
  • The task does not run when the laptop is on battery. โ†’ Open the task’s Properties > Conditions and uncheck Start the task only if the computer is on AC power.
  • Scripts that use Outlook or Excel fail. โ†’ These apps need you to be logged in. Keep the option Run only when user is logged on.

Wrap-up

Now your script works even when you forget it. The log file tells you what happened, and the .bat wrapper keeps everything in one place.

Next, try adding a second trigger in the task’s Triggers tab, like “At log on”. Then the script also runs every time you start your computer. Happy automating!