Programmatic way of Creating Task in Windows Task Scheduler

Ajay Bile
3 min readApr 20, 2020

--

So, finally I decided to write a blog. It took me one day to decide topic for my first blog, but now here i am writing my first technical blog. I welcome you all…

So with that today I will be discussing how one can create a task in windows task scheduler using python. I have experience in python so i have chosen this programming language. Here will go with flow of question and answers, as its a better way of understanding.

What is Task Scheduler ?

Task scheduler is tool of Microsoft Windows that provides the ability to schedule the launch of programs or scripts at predefined times or after specified time intervals or if certain set of condition is met. For example, you can schedule a task to run a backup script every night, or it will send you an email whenever a certain system event occurs.

What is COM Object and why they are so important in windows automation ?

COM stands for Component Object Model. COM objects allows us to control windows application from another program.

COM objects are defined in Windows registry. In normal OOPS concepts class defines the implementation and object is an instance of an class, so with that concept, windows registry already has implementation hence “class” and COM objects are “objects” which are accessible to our script, hence we can say that COM objects gives us a programmable interface that can be used by our program so we can control windows applications.

Although the term “class” doesn’t refer to a Python class, only the concept is identical.

Here classes are registered unique class ID (CLSID) and a program ID (ProgID). For example, Windows Task Scheduler defines its ProgID as Schedule.Service and Microsoft Excel defines its ProgID as Excel.Application etc.

What is win32com library ?

win32com is basically a very thin wrapper of python that allows us to interact with COM objects and automate Windows applications with python.
With this approach you can pretty much do anything that a Microsoft Application can do through python.

Layer wise structure
Layers of Interactions

Python Code for creating a task in task scheduler which will execute daily after every 5 minutes for next 10 days.

import datetime
import win32com.client

scheduler = win32com.client.Dispatch('Schedule.Service')
scheduler.Connect()
root_folder = scheduler.GetFolder('\\')
task_def = scheduler.NewTask(0)

# Defining the Start time of job
start_time = datetime.datetime.now() + datetime.timedelta(minutes=1)

# For Daily Trigger set this variable to 2 ; for One time run set this value as 1
TASK_TRIGGER_DAILY = 2
trigger = task_def.Triggers.Create(TASK_TRIGGER_DAILY)

#Repeat for a duration of number of day
num_of_days = 10
trigger.Repetition.Duration = "P"+str(num_of_days)+"D"

#use PT2M for every 2 minutes, use PT1H for every 1 hour
trigger.Repetition.Interval = "PT2M"
trigger.StartBoundary = start_time.isoformat()

# Create action
TASK_ACTION_EXEC = 0
action = task_def.Actions.Create(TASK_ACTION_EXEC)
action.ID = 'TRIGGER BATCH'
action.Path = 'cmd.exe'
action.Arguments ='/c start "" "C:\\Ajay\\Desktop\\test.bat"'

# Set parameters
task_def.RegistrationInfo.Description = 'Test Task'
task_def.Settings.Enabled = True
task_def.Settings.StopIfGoingOnBatteries = False

# Register task
# If task already exists, it will be updated
TASK_CREATE_OR_UPDATE = 6
TASK_LOGON_NONE = 0
root_folder.RegisterTaskDefinition(
'Test Task', # Task name
task_def,
TASK_CREATE_OR_UPDATE,
'', # No user
'', # No password
TASK_LOGON_NONE
)

Guys I hope thi small tutorial has helped you. I will be discussing projects, concepts, architecture on AI,ML, Automation technologies in my future blogs. Please like share and subscribe.

“Alone we can do so little; together we can do so much.”

Thank you.

--

--

Ajay Bile

I learn everyday 😎 Python-SQL-Big Data-Data Engineering-Process Automation