Admin Power Level: Intermediate Chapter 12: Let the Computer Do the Work on Time
Create a scheduled task
Creating a task means describing four things: what runs, when it runs, who it runs as, and under what conditions. PowerShell builds each part separately, then registers the task.
Lesson 2 of 2 in Chapter 12: Let the Computer Do the Work on Time. Previous step in this path: Show, disable, and enable scheduled tasks. 3 learning objectives, 8 examples, and a knowledge check.
Start: what you will learn
Creating a task means describing four things: what runs, when it runs, who it runs as, and under what conditions. PowerShell builds each part separately, then registers the task.
Learning objectives
- Explain trigger, action, principal, and settings
- Build a task that runs a script daily
- Register a task and confirm it exists
Concept in simple words
What it is: creating a scheduled task means telling Windows four things: what program or script runs, when it runs, which account runs it, and how it should behave if the computer is busy or misses a run. PowerShell builds each part separately, then joins them into one task. Nothing runs on a schedule until you register the task, and nothing is automatic just because you typed a command. Registration is the moment the computer accepts the job and starts watching for its trigger.
Why it works
Why it exists: some work must happen on time, every time, even when you are asleep, away, or busy with other work. A daily backup, a weekly report, and a regular cleanup are classic examples. Doing them by hand every day wastes time and invites mistakes, because a busy day means a skipped day. A scheduled task removes the human memory step. You set the rule once, you test it once, and then the computer follows the rule. That is the whole point of this chapter, and it is why testing matters so much. A schedule will faithfully repeat a good job, and it will also faithfully repeat a broken job.
What PowerShell is doing behind the command: New-ScheduledTaskAction, New-ScheduledTaskTrigger, New-ScheduledTaskPrincipal, and New-ScheduledTaskSettingsSet do not create a task on their own. Each one builds a small object in memory and stores it in a variable, such as $action or $trigger. At that point, nothing is saved and nothing will run. Register-ScheduledTask then takes those objects, joins them under a TaskName, and saves the full definition with the Task Scheduler service. That split is deliberate. It lets you check one part at a time, fix one part without rebuilding everything, and read the saved task back after registration.
The action, in depth: the action says what runs. New-ScheduledTaskAction needs -Execute for the program, such as powershell.exe, and -Argument for what to pass to that program, such as -File followed by the full script path. The program and its argument are two different things, and confusing them is a common beginner error. The -Execute part must be a program Windows can start. The -Argument part tells that program what to do. You can also add -WorkingDirectory, which sets the folder the program starts in. That matters when a script uses relative paths, because a scheduled task may start in a different folder from the folder you used when you tested by hand.
The trigger, in depth: the trigger says when the task starts. New-ScheduledTaskTrigger has a switch for the kind of schedule, such as -Daily, -Weekly, -Once, -AtLogon, or -AtStartup, plus -At for the time. A daily trigger repeats every day at the same time, and -DaysInterval can stretch it to every second or third day. A weekly trigger needs -DaysOfWeek to name the days, and -WeeksInterval can stretch it to every second week. A Once trigger runs a single time at the date and time in -At. An AtLogon trigger runs when a user logs on, and an AtStartup trigger runs when the system starts. One task can have more than one trigger, and the task starts when any of its triggers fires. That is why a task with no trigger never runs, no matter how good its action is.
The principal, in depth: the principal says who the task runs as. New-ScheduledTaskPrincipal uses -UserId for the account, -LogonType for how that account logs on, and -RunLevel for the privilege level. For example, -UserId "NT AUTHORITY\SYSTEM" with -LogonType ServiceAccount runs the task as the SYSTEM account, which is a built in system account, not a person. -RunLevel Highest asks for elevated privileges, which a task may need if its script changes system settings or protected files. The principal matters because the same script can work for you by hand and fail on a schedule, if the scheduled account cannot read the script, cannot reach a file, or does not have the rights the job needs.
The settings, in depth: settings say how the task should behave around the edges. New-ScheduledTaskSettingsSet can set -AllowStartIfOnBatteries and -DontStopIfGoingOnBatteries, so a laptop task is not blocked just because it is on battery power. It can set -StartWhenAvailable, so a task that missed its time because the computer was off still starts when the computer comes back. It can set -ExecutionTimeLimit, so a stuck task does not run forever, and -RestartCount with -RestartInterval, so a failed task retries a set number of times after a set wait. You do not need every setting for a first task, but you do need to know that settings are where missed runs, retries, and time limits live.
Think of it like this
Think of it like this: setting an alarm with a note attached. The alarm is the trigger, the note says what to do when it rings, and the name on the alarm tells you whose job it is. If any part is missing, the alarm either never rings or rings with no job to do, and that is exactly what happens with a task that has a trigger but no action, or an action but no trigger. Add the principal and the settings to the picture and the analogy still works. The principal is who is trusted to do the job when the alarm rings, and the settings are the small print, such as what to do if the alarm rings while you are out, or how long the job is allowed to take.
How it works, step by step
How it works, step by step: First, build the action, usually a program plus its arguments, such as a script path. Second, build the trigger, such as daily at a set time, or weekly on a chosen day. Third, choose the account and settings if your task needs them, for example whether it should run when nobody is logged on. Fourth, register the task with a clear name, then read it back to confirm it exists. Fifth, run it once on demand with Start-ScheduledTask to test it, before you trust the schedule. Sixth, check the result with Get-ScheduledTaskInfo, and only then leave the task to run on its own.
Safety warning, read this before you register anything: a scheduled task runs automatically, without you watching it. That is powerful, and that is the risk. Test on your own computer first, with a harmless test script, before you schedule anything real. Never schedule a task that changes, moves, or deletes data until you have a backup and you have run the script by hand, in the same way, and checked its result. If a script deletes the wrong folder by hand, you see it at once. If the same script is on a schedule, it can repeat the damage every day before anyone notices. A test run is not optional polish. It is the safety step.
Let us walk through example 1, the daily task: $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\backup.ps1". This line stores an action object in $action. The -Execute part names the program, powershell.exe. The -Argument part passes -File and the full script path to that program, so PowerShell knows to run that script file. Next, $trigger = New-ScheduledTaskTrigger -Daily -At 2am stores a daily trigger in $trigger, with -Daily saying every day and -At 2am giving the time. Finally, Register-ScheduledTask -TaskName "DailyBackup" -Action $action -Trigger $trigger joins those two objects under the name DailyBackup. In the output, look for the TaskName and a State of Ready, which means Windows accepted the task and is waiting for its trigger.
Let us walk through example 2, the test run: Start-ScheduledTask -TaskName "DailyBackup". This command starts the task once, right now, instead of waiting for 2am. It does not change the schedule. The daily trigger is still there for tomorrow. The point is proof. If the script path is wrong, if the account cannot read the script, or if the script fails, you find out now, in a test you are watching, instead of finding out days later when a backup is missing. After the test, check Get-ScheduledTaskInfo for LastRunTime and LastTaskResult, and check whatever the script was supposed to produce, such as a backup file in a test folder.
Let us walk through example 3, the weekly task: the first line builds an action that runs a script called weekly-report.ps1. The second line builds a weekly trigger for Monday at 9am, where -DaysOfWeek names the day and -At gives the time. The third line registers a task called WeeklyReport from those two parts, using the same action and trigger pattern as the daily example in the lesson. In the output, look for TaskName WeeklyReport and State Ready. What changes if you change one part? Change Monday to Friday and the report moves to Friday. Change -Weekly to -Daily and the same script runs every day instead of once a week. Change the script path and you run a different script, so always read the action back after a change.
Let us walk through example 4, the logon task: $trigger = New-ScheduledTaskTrigger -AtLogon. This trigger has no -At time, because it does not fire on a clock. It fires when a user logs on. That makes it the right choice for setup work that should happen at the start of a session, and the wrong choice for work that must happen at 2am when nobody is logged on. The action and the Register-ScheduledTask line follow the same pattern as the daily task. In the output, look for State Ready, and remember that Ready for a logon task means waiting for a logon, not waiting for a time.
Let us walk through example 5, the full task with principal and settings: first, $principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest builds the run as part, using the SYSTEM account as a service account with the highest run level. Next, $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable builds settings that let the task start on battery power, keep going on battery power, and start late if its time was missed. Finally, Register-ScheduledTask adds -Principal $principal, -Settings $settings, and -Description, which is a short plain words note about what the task does. In the output, look for TaskName and State Ready, and read the task back to confirm the parts you added are really there.
Let us walk through example 6, the repeating Once trigger: $trigger = New-ScheduledTaskTrigger -Once -At 9am -RepetitionInterval (New-TimeSpan -Minutes 30) -RepetitionDuration (New-TimeSpan -Hours 4). The -Once part sets a single start time, 9am. The -RepetitionInterval part says repeat every 30 minutes, and -RepetitionDuration says keep repeating for 4 hours. New-TimeSpan builds those time lengths, because the repetition parameters need a time span object, not just a number. Change the interval to 60 minutes and the task runs half as often. Change the duration to 2 hours and the repeats stop sooner. If the duration is shorter than the interval, you may get only the first run, so always check that the duration covers the repeats you want.
Let us walk through example 7, reading the action back: (Get-ScheduledTask -TaskName "DailyBackup").Actions | Select-Object Execute, Arguments. Get-ScheduledTask fetches the saved task, and .Actions picks out its action list. Select-Object then shows Execute and Arguments, which are the program and what was passed to it. This is how you prove the saved task runs what you think it runs. If Execute shows the wrong program, or Arguments shows the wrong script path, fix the task now, before the schedule fires. Do not trust your memory of what you typed. Trust the saved definition.
Let us walk through example 8, cleaning up your own test task: Unregister-ScheduledTask -TaskName "DailyBackup" -Confirm:$false. Unregister removes the task definition from Task Scheduler. The -Confirm:$false part skips the confirmation prompt, so the task is removed at once. Only use this on a test task you created yourself, and only after you are sure you no longer need it. Never unregister a system task, and never use -Confirm:$false on a task you have not just listed and identified. If you only need a pause, use Disable-ScheduledTask from the previous lesson instead, because delete cannot be undone by enabling the task again.
How to spot a failure, even when the task looks fine: State Ready only means the task is waiting. It never proves the last run worked. After a test run, use Get-ScheduledTaskInfo and look at LastRunTime and LastTaskResult. A result of zero usually means success. A non zero result means something failed, and you need to look at the script and its output next. Also check NextRunTime. If a daily task shows no next run time, check whether it is disabled, whether its trigger is in the past, or whether the task was removed. The schedule, the last result, and the next run time together tell the true story.
When to use it
When to use it, and when not to: use a scheduled task for work that repeats on a clock, such as a daily backup, a weekly report, or a cleanup job. Do not use it for a job you only need once, unless that once is a timed Once trigger you set on purpose, and do not use it for a job that needs you to answer questions while it runs, because a scheduled task runs without you there. Also, do not schedule a task that runs a script you have not tested by hand first, because a schedule will faithfully repeat a broken script at the same time every day. And do not schedule a task that needs files only you can reach, unless the principal you chose can reach those files too.
Common mistakes and edge cases to watch for: the most common is registering a trigger with no action, or an action with no trigger, and then wondering why nothing happens. Both parts are required. Another is a wrong script path, because Register-ScheduledTask does not check that your script path is correct, so a typo only shows up at test time. A third is a relative path that worked by hand but fails on a schedule, because the working directory is different. Use full paths, and use -WorkingDirectory when the script expects a starting folder. A fourth is reusing a name you already used, and overwriting a task you meant to keep, especially if you add -Force, which skips the confirmation prompt.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Register-ScheduledTask -TaskName <Name> -Action <Action> -Trigger <Trigger>Example 1
Builds an action and a daily trigger, then registers a task called DailyBackup.
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\backup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "DailyBackup" -Action $action -Trigger $triggerIllustrative output: TaskName State -------- ----- DailyBackup Ready
Example 2
Runs the task once, on demand, so you can test it before the schedule fires.
Start-ScheduledTask -TaskName "DailyBackup"Illustrative output: Task DailyBackup started.
Example 3
The first line builds an action that runs a script called weekly-report.ps1. The second line builds a weekly trigger for Monday at 9am, where -DaysOfWeek names the day and -At gives the time. The third line registers a task called WeeklyReport from those two parts, using the same action and trigger pattern as the daily example in the lesson.
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\weekly-report.ps1"
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 9am
Register-ScheduledTask -TaskName "WeeklyReport" -Action $action -Trigger $triggerIllustrative output: TaskName State -------- ----- WeeklyReport Ready
Example 4
Builds a logon trigger with no clock time, so the task runs when a user logs on, then registers it as LogonNote.
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\logon-note.ps1"
$trigger = New-ScheduledTaskTrigger -AtLogon
Register-ScheduledTask -TaskName "LogonNote" -Action $action -Trigger $triggerIllustrative output: TaskName State -------- ----- LogonNote Ready
Example 5
Adds a SYSTEM principal with the highest run level and settings for battery power and missed starts, then registers the full task with a plain words description.
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\backup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
$principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
Register-ScheduledTask -TaskName "DailyBackupFull" -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Description "Daily backup task"Illustrative output: TaskName State -------- ----- DailyBackupFull Ready
Example 6
Starts once at 9am, then repeats every 30 minutes for 4 hours. New-TimeSpan builds the interval and the duration as time span objects.
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\check.ps1"
$trigger = New-ScheduledTaskTrigger -Once -At 9am -RepetitionInterval (New-TimeSpan -Minutes 30) -RepetitionDuration (New-TimeSpan -Hours 4)
Register-ScheduledTask -TaskName "MorningCheck" -Action $action -Trigger $triggerIllustrative output: TaskName State -------- ----- MorningCheck Ready
Example 7
Reads the saved action back, so you can confirm the program and the script path are exactly what you intended.
(Get-ScheduledTask -TaskName "DailyBackup").Actions | Select-Object Execute, ArgumentsIllustrative output: Execute Arguments ------- --------- powershell.exe -File C:\Scripts\backup.ps1
Example 8
Removes only your own test task DailyBackup, with no confirmation prompt. Use this only after you have listed and identified the task, and never for a system task. For a pause, use Disable-ScheduledTask instead.
Unregister-ScheduledTask -TaskName "DailyBackup" -Confirm:$falseIllustrative output: Task DailyBackup removed.
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Creating a task that runs a script, without checking the script path and execution policy on that account.
- Registering a task with a trigger but no action, or an action but no trigger, then wondering why nothing happens.
- Registering a task and waiting for its scheduled time to find out it failed, instead of running it once on demand with Start-ScheduledTask to test it right away.
- Writing the script path in -Argument without -File, so PowerShell does not run the script file as intended. The argument must name -File followed by the full script path.
- Using a relative script path that worked by hand but fails on a schedule, because the starting folder is different. Use full paths, and set -WorkingDirectory when the script expects a starting folder.
- Reusing a task name you already used, and overwriting a task you meant to keep, especially with -Force, which skips the confirmation prompt. List first, and pick a clear new name for a new task.
- Scheduling a task that changes, moves, or deletes data without a backup and a test run first. Test on your own computer with a harmless script, check the result, and only then schedule the real job.
- Choosing a principal that cannot read the script or reach the files it needs, or leaving RunLevel too low for a job that needs elevation. Test with the same principal you plan to schedule, not just with your own session.
- Trusting State Ready as proof of success. Ready only means waiting. Check LastRunTime and LastTaskResult with Get-ScheduledTaskInfo after every test run.
Knowledge check
Knowledge check
Which part of a task says what program runs?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: action says what, trigger says when, account says who, and settings say how. Build the parts, register them, read the task back, and test it once on demand, and you will catch most problems before the schedule ever fires. Never schedule a job that changes or deletes data without a backup and a test run first, and never trust State Ready alone. Check the saved action, check the last result, and check the next run time, and your scheduled task will be a helper you can trust, not a surprise waiting at 2am.
Next step: Firewall profiles, rules, and connection profiles