How to Use Ansible in Windows: Windows Scheduled Tasks
Step-by-Step Guide to Managing Windows Scheduled Tasks Using Ansible

👋 Hi, I’m Hardik, a DevOps Engineer passionate about automation, cloud-native systems, and building reliable infrastructure. 🗣️I can speak: German, English, Hindi, and Sindhi
In this post of the Ansible 4 Windows series, we will learn how to create, remove, disable, and restart scheduled tasks on Windows servers using Ansible.
If you’ve worked with Windows before, you already know how useful scheduled tasks can be, they allow you to automate actions such as running scripts, opening applications, or triggering maintenance jobs at specific times. Now, with Ansible, you can manage these tasks consistently across multiple servers, without having to log into each one manually.
By the end of this guide, you will know how to:
Create a scheduled task
Remove a scheduled task
Disable a scheduled task (with PowerShell support)
Restart a scheduled task (with PowerShell support)
Let’s walk through each of these step by step.
Prerequisites
Before we begin, make sure you have the following:
A working Ansible control node (Linux).
One or more Windows servers configured as managed hosts.
Ansible collections installed for Windows:
ansible-galaxy collection install community.windows ansible.windowsBasic familiarity with running Ansible playbooks.
For disabling and restarting tasks, we will also need two small PowerShell scripts placed inside the Windows server. You can either:
Manually copy them to the server (e.g.,
C:\Users\Ansible\Scripts\), orUse Ansible’s
ansible.windows.win_copymodule to copy them over from your control node.
We’ll cover the scripts later in this post.
1. Creating a Scheduled Task
Here’s a simple playbook that creates a scheduled task. This task will run two commands:
Show the computer’s hostname
Show the user running the task
---
- name: Create a scheduled task
hosts: windows
gather_facts: true
tasks:
- name: Create <YourTaskName> to run cmd commands
community.windows.win_scheduled_task:
name: <YourTaskName>
description: open command prompt
actions:
- path: cmd.exe
arguments: /c hostname
- path: cmd.exe
arguments: /c whoami
triggers:
- type: daily
start_boundary: '2025-08-21T15:20:00'
username: <YourAnsibleUser> # You can also use "SYSTEM" as username here.
state: present
enabled: true
What this does:
Creates a task with the name
<YourTaskName>Runs two commands (
hostnameandwhoami) daily at 3:20 PM.Executes as the
<YourAnsibleUser>ORSYSTEMuser.
2. Removing a Scheduled Task
If you no longer need the task, you can remove it with the following playbook:
---
- name: Remove a scheduled task
hosts: windows
gather_facts: true
tasks:
- name: Remove <YourTaskNAme>
community.windows.win_scheduled_task:
name: <YourTaskNAme>
state: absent
This deletes the scheduled task called <YourTaskNAme> from the Windows server.
3. Disabling a Scheduled Task (PowerShell)
Sometimes you don’t want to delete a task but simply disable it. Ansible doesn’t directly provide a disable option, so we will use a PowerShell script to handle this.
First, create a script called Disable-Task.ps1 with the following content:
Param(
[Parameter(Mandatory=$true)]
[string]$TaskName
)
try {
$task = Get-ScheduledTask -TaskName $TaskName -ErrorAction Stop
Disable-ScheduledTask -InputObject $task
Write-Host "Task '$TaskName' has been disabled successfully."
}
catch {
Write-Error "Failed to disable task '$TaskName'. Error: $_"
}
Save this file under:C:\Users\Ansible\Scripts\Disable-Task.ps1
Now, use the playbook below to run the script from Ansible:
---
- name: Disable Windows Scheduled Task
hosts: windows
gather_facts: true
vars:
task_name: "<YourTaskName>"
script_path: "C:\\Users\\<YourAnsibleUser>\\Scripts\\Disable-Task.ps1"
tasks:
- name: Disable the scheduled task
ansible.windows.win_shell: |
powershell.exe -ExecutionPolicy Bypass -File "{{ script_path }}" -TaskName "{{ task_name }}"
This will disable the task named <YourTaskNAme>.
4. Restarting a Scheduled Task (PowerShell)
To restart a scheduled task, we will use another PowerShell script:
Param(
[Parameter(Mandatory=$true)]
[string]$TaskName
)
try {
$task = Get-ScheduledTask -TaskName $TaskName -ErrorAction Stop
if ($task.State -eq 'Disabled') {
Enable-ScheduledTask -InputObject $task
Write-Host "Task '$TaskName' was disabled and has been enabled."
}
$taskState = (Get-ScheduledTaskInfo -TaskName $TaskName).State
if ($taskState -eq 'Running') {
Stop-ScheduledTask -InputObject $task
Write-Host "Task '$TaskName' was running and has been stopped."
}
Start-ScheduledTask -InputObject $task
Write-Host "Task '$TaskName' has been started successfully."
}
catch {
Write-Error "Failed to restart task '$TaskName'. Error: $_"
}
Save it as:C:\Users\Ansible\Scripts\Restart-Task.ps1
Here’s the Ansible playbook to call this script:
---
- name: Restart Windows Scheduled Task
hosts: windows
gather_facts: true
vars:
task_name: "<YourTaskName>"
script_path: "C:\\Users\\<YourAnsibleUser>\\Scripts\\Restart-Task.ps1"
tasks:
- name: Restart the scheduled task
ansible.windows.win_shell: |
powershell.exe -ExecutionPolicy Bypass -File "{{ script_path }}" -TaskName "{{ task_name }}"
Wrapping Up
We have now covered how to:
Create a scheduled task using Ansible modules.
Remove a scheduled task when no longer needed.
Disable a task temporarily using PowerShell scripts.
Restart a scheduled task and bring it back into action.
Scheduled tasks are powerful for automating routine jobs on Windows servers. By combining them with Ansible, you gain the ability to manage these tasks consistently across multiple machines, saving time and reducing manual effort.




