Scheduling scripts lets your computer run tasks automatically at set times. This saves you from doing repetitive work manually.
Scheduled scripts with Task Scheduler in PowerShell
Introduction
Syntax
PowerShell
schtasks /create /tn "TaskName" /tr "powershell.exe -File C:\Path\To\Script.ps1" /sc schedule_type /st HH:mm
/tn is the task name you choose.
/tr is the command to run your script.
Examples
PowerShell
schtasks /create /tn "DailyBackup" /tr "powershell.exe -File C:\Scripts\backup.ps1" /sc daily /st 23:00
PowerShell
schtasks /create /tn "WeeklyCleanup" /tr "powershell.exe -File C:\Scripts\cleanup.ps1" /sc weekly /d MON /st 02:00
Sample Program
This simple PowerShell script prints a message. You can schedule it with Task Scheduler to see it run automatically.
PowerShell
Write-Output "Hello! This script runs on schedule."Important Notes
Make sure your script path is correct and accessible.
Use 24-hour time format for scheduling (e.g., 14:30 for 2:30 PM).
Run Task Scheduler as administrator to create tasks.
Summary
Task Scheduler runs scripts automatically at set times.
Use schtasks /create with task name, script path, schedule, and start time.
Scheduling saves time and avoids forgetting important tasks.
Practice
1. What is the main purpose of using Task Scheduler with PowerShell scripts?
easy
Solution
Step 1: Understand Task Scheduler's role
Task Scheduler is designed to run tasks automatically based on a schedule.Step 2: Identify the benefit for scripts
Running scripts automatically saves time and ensures tasks run without forgetting.Final Answer:
To run scripts automatically at specific times without manual start -> Option DQuick 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
Solution
Step 1: Identify the command to create a task
The/createoption is used to create a new scheduled task.Step 2: Check the task action and schedule
The task runs PowerShell with the script file path, scheduled daily at 07:00.Final Answer:
schtasks /create /tn "MyTask" /tr "powershell.exe -File C:\Scripts\task.ps1" /sc daily /st 07:00 -> Option AQuick 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?
Assuming the task "BackupTask" exists and is scheduled.
schtasks /query /tn "BackupTask"
Assuming the task "BackupTask" exists and is scheduled.
medium
Solution
Step 1: Understand the /query option
The/queryoption lists information about scheduled tasks.Step 2: Using /tn with /query
Specifying/tn "BackupTask"filters the query to show only that task's details.Final Answer:
Displays details of the scheduled task named "BackupTask" -> Option CQuick 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:
But the task does not run at 9 AM. What is the likely error?
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
Solution
Step 1: Check the /tr argument syntax
The PowerShell command should include-Filebefore the script path to run it properly.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.Final Answer:
Missing the -File parameter before the script path in /tr -> Option BQuick 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
Solution
Step 1: Choose correct schedule type for specific weekdays
The/sc weeklyoption schedules tasks weekly on specified days.Step 2: Specify days and time correctly
Use/d MON,FRIto run on Monday and Friday, and/st 18:00for 6 PM start time.Final Answer:
schtasks /create /tn "WeeklyTask" /tr "powershell.exe -File C:\Scripts\weekly.ps1" /sc weekly /d MON,FRI /st 18:00 -> Option AQuick 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
