Bird
Raised Fist0
PowerShellscripting~10 mins

Scheduled scripts with Task Scheduler in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Scheduled scripts with Task Scheduler
Write PowerShell Script
Open Task Scheduler
Create New Task
Set Trigger (time/event)
Set Action (run script)
Save Task
Task Runs Automatically
Script Output Happens
You write a script, create a scheduled task with triggers and actions, then the task runs the script automatically at set times.
Execution Sample
PowerShell
Write-Output "Hello, Task Scheduler!"

# Save as HelloTask.ps1

# In Task Scheduler:
# Action: powershell.exe -File "C:\Scripts\HelloTask.ps1"
# Trigger: Daily at 9 AM
This script prints a message. Task Scheduler runs it daily at 9 AM automatically.
Execution Table
StepActionDetailsResult
1Write scriptCreate HelloTask.ps1 with Write-Output commandScript file saved
2Open Task SchedulerLaunch Task Scheduler appReady to create task
3Create new taskName: HelloTask, Trigger: Daily 9 AMTask created but not running yet
4Set actionRun powershell.exe with script file pathAction linked to task
5Save taskConfirm and save task settingsTask scheduled
6Trigger time reached9 AM daily arrivesTask starts running
7Task runs scriptpowershell.exe runs HelloTask.ps1Output: Hello, Task Scheduler!
8Task completesScript finishes executionTask ends, waits for next trigger
💡 Task Scheduler waits for next trigger after script completes
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7Final
ScriptFileNoneHelloTask.ps1 createdPath set in task actionScript executedReady for next run
TaskStatusNoneNot createdCreated and configuredRunningIdle, waiting
Key Moments - 3 Insights
Why doesn't the script run immediately after creating the task?
Because Task Scheduler runs the script only when the trigger time or event occurs, as shown in execution_table step 6.
How does Task Scheduler know which script to run?
The script path is set in the task's action settings, as seen in execution_table step 4.
What happens if the script file is moved after scheduling the task?
The task will fail to run the script because the path is broken, so the output won't appear at step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the task actually scheduled to run automatically?
AStep 5
BStep 3
CStep 6
DStep 7
💡 Hint
Check when the task is saved and ready to run automatically (step 5).
According to the variable tracker, what is the status of the task after step 7?
ANot created
BIdle, waiting
CRunning
DFailed
💡 Hint
Look at TaskStatus after step 7 in variable_tracker.
If you change the trigger time to 10 AM, which step in the execution table changes?
AStep 3
BStep 6
CStep 7
DStep 8
💡 Hint
The trigger time affects when the task starts running (step 6).
Concept Snapshot
Scheduled scripts with Task Scheduler:
- Write your PowerShell script and save it.
- Open Task Scheduler and create a new task.
- Set a trigger (time/event) and an action (run your script).
- Save the task; it runs automatically at trigger time.
- Script output happens when the task runs.
- Task Scheduler waits for the next trigger after completion.
Full Transcript
This visual execution shows how to schedule a PowerShell script using Task Scheduler. First, you write a simple script that outputs a message. Then, you open Task Scheduler and create a new task, setting a trigger like daily at 9 AM and an action to run your script with powershell.exe. After saving, the task waits until the trigger time. When 9 AM arrives, Task Scheduler runs the script automatically, and you see the output. After the script finishes, the task waits for the next scheduled time. Variables like the script file path and task status change as you progress through these steps. Common confusions include why the script doesn't run immediately and how Task Scheduler knows which script to run. The quizzes help reinforce understanding of when the task is scheduled, task status during execution, and how changing trigger times affects execution.

Practice

(1/5)
1. What is the main purpose of using Task Scheduler with PowerShell scripts?
easy
A. To write scripts faster using a graphical interface
B. To convert scripts into executable files
C. To debug scripts interactively
D. To run scripts automatically at specific times without manual start

Solution

  1. Step 1: Understand Task Scheduler's role

    Task Scheduler is designed to run tasks automatically based on a schedule.
  2. Step 2: Identify the benefit for scripts

    Running scripts automatically saves time and ensures tasks run without forgetting.
  3. Final Answer:

    To run scripts automatically at specific times without manual start -> Option D
  4. Quick Check:

    Task Scheduler automates script running [OK]
Hint: Task Scheduler automates script runs on schedule [OK]
Common Mistakes:
  • Thinking Task Scheduler helps write or debug scripts
  • Confusing automation with script editing
  • Assuming it converts scripts to executables
2. Which of the following is the correct syntax to create a scheduled task that runs a PowerShell script daily at 7 AM using schtasks?
easy
A. schtasks /create /tn "MyTask" /tr "powershell.exe -File C:\Scripts\task.ps1" /sc daily /st 07:00
B. schtasks /run /tn "MyTask" /tr "powershell.exe -File C:\Scripts\task.ps1" /sc daily /st 07:00
C. schtasks /delete /tn "MyTask" /tr "powershell.exe -File C:\Scripts\task.ps1" /sc daily /st 07:00
D. schtasks /create /tn "MyTask" /tr "C:\Scripts\task.ps1" /sc hourly /st 07:00

Solution

  1. Step 1: Identify the command to create a task

    The /create option is used to create a new scheduled task.
  2. Step 2: Check the task action and schedule

    The task runs PowerShell with the script file path, scheduled daily at 07:00.
  3. Final Answer:

    schtasks /create /tn "MyTask" /tr "powershell.exe -File C:\Scripts\task.ps1" /sc daily /st 07:00 -> Option A
  4. Quick Check:

    Use /create with /tn, /tr, /sc daily, /st 07:00 [OK]
Hint: Use /create to schedule, not /run or /delete [OK]
Common Mistakes:
  • Using /run instead of /create to schedule
  • Omitting powershell.exe in /tr argument
  • Wrong schedule type like hourly instead of daily
3. What will be the output when running this command?
schtasks /query /tn "BackupTask"

Assuming the task "BackupTask" exists and is scheduled.
medium
A. Deletes the task named "BackupTask"
B. Creates a new task named "BackupTask"
C. Displays details of the scheduled task named "BackupTask"
D. Shows an error because /query cannot be used with /tn

Solution

  1. Step 1: Understand the /query option

    The /query option lists information about scheduled tasks.
  2. Step 2: Using /tn with /query

    Specifying /tn "BackupTask" filters the query to show only that task's details.
  3. Final Answer:

    Displays details of the scheduled task named "BackupTask" -> Option C
  4. Quick Check:

    /query with /tn shows task info [OK]
Hint: Use /query with /tn to see specific task info [OK]
Common Mistakes:
  • Confusing /query with /create or /delete
  • Thinking /query with /tn causes error
  • Expecting task creation or deletion output
4. You wrote this command to schedule a script:
schtasks /create /tn "DailyReport" /tr "powershell.exe C:\Scripts\report.ps1" /sc daily /st 09:00

But the task does not run at 9 AM. What is the likely error?
medium
A. Using /sc daily instead of /sc hourly
B. Missing the -File parameter before the script path in /tr
C. Task name "DailyReport" is invalid
D. Start time format 09:00 is incorrect

Solution

  1. Step 1: Check the /tr argument syntax

    The PowerShell command should include -File before the script path to run it properly.
  2. Step 2: Understand why missing -File causes failure

    Without -File, PowerShell does not know to execute the script file, so the task runs but does nothing.
  3. Final Answer:

    Missing the -File parameter before the script path in /tr -> Option B
  4. Quick Check:

    PowerShell needs -File to run script [OK]
Hint: Always include -File before script path in /tr [OK]
Common Mistakes:
  • Omitting -File in PowerShell command
  • Changing schedule type unnecessarily
  • Assuming task name or time format is wrong
5. You want to schedule a PowerShell script to run every Monday and Friday at 6 PM. Which schtasks command correctly sets this up?
hard
A. schtasks /create /tn "WeeklyTask" /tr "powershell.exe -File C:\Scripts\weekly.ps1" /sc weekly /d MON,FRI /st 18:00
B. schtasks /create /tn "WeeklyTask" /tr "powershell.exe -File C:\Scripts\weekly.ps1" /sc daily /d MON,FRI /st 18:00
C. schtasks /create /tn "WeeklyTask" /tr "powershell.exe -File C:\Scripts\weekly.ps1" /sc weekly /d 1,5 /st 18:00
D. schtasks /create /tn "WeeklyTask" /tr "powershell.exe -File C:\Scripts\weekly.ps1" /sc monthly /d MON,FRI /st 18:00

Solution

  1. Step 1: Choose correct schedule type for specific weekdays

    The /sc weekly option schedules tasks weekly on specified days.
  2. Step 2: Specify days and time correctly

    Use /d MON,FRI to run on Monday and Friday, and /st 18:00 for 6 PM start time.
  3. Final Answer:

    schtasks /create /tn "WeeklyTask" /tr "powershell.exe -File C:\Scripts\weekly.ps1" /sc weekly /d MON,FRI /st 18:00 -> Option A
  4. Quick Check:

    Weekly schedule with MON,FRI days and 18:00 time [OK]
Hint: Use /sc weekly with /d MON,FRI for specific weekdays [OK]
Common Mistakes:
  • Using /sc daily instead of weekly for specific days
  • Using numeric days instead of MON,FRI
  • Choosing monthly schedule incorrectly