Skip to content
Menu

Create or Update a Scheduled Task

Registers one scheduled task with one trigger and one action, as SYSTEM or for signed-in users. Rerunning with the same inputs changes nothing, and an update saves the old task first.

Create or update a scheduled task is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Create or update a scheduled task. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, Windows 11, Windows Server 2016+; runs as SYSTEM
NeedsPowerShell 5.1, administrator rights
Changes thingsChanges what it runs on

What It Does

The script registers one scheduled task in the root folder of Task Scheduler with one trigger and one action, and nothing else. It never runs the task, never deletes a task and never touches another one. The task is defined by TaskName, the program in Command and its Arguments, a Trigger (Startup, Logon, Daily or Once, with At for the last two), who it runs as (System, or Users, the BUILTIN\Users group, so it runs in the session of whoever signs in), and whether it is Hidden. Startup requires System. For Once, At can also be a future date and time.

Running it again with the same inputs changes nothing and says OK. With different inputs it updates the task, and the old definition is exported to XML and copied to the MSPOutpost backup folder first. TaskName must start with MSPOutpost- so this script only ever manages its own tasks. Set AllowAnyName to true to use another name, which can replace an existing task of that name after the backup.

A task that runs as System executes its program with full rights, so when RunAs is System the script refuses a program, or a file named in Arguments, that standard users could change. Fix the permissions or put the program under C:\Program Files or C:\Windows.

The Command is stored as a full path, and a bare name such as powershell.exe is resolved first. A .ps1 file is refused: run powershell.exe with Arguments -NoProfile -File and the script path instead.

Before You Run It

  • Keep the MSPOutpost- prefix so your tasks are easy to find and the script manages only its own.
  • Put the program under Program Files or Windows if it runs as System. Writable paths are refused.
  • Use the full path in Command where you can. Daily uses 24-hour local time in At. Once accepts that or a future local date and time.
  • Run it with DryRun=true first to see whether it would create or update the task.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
  • What it changes: Creates or updates one scheduled task; saves the old definition as XML before an update.
  • Restart: It never restarts the machine.
  • Typical run: Under 10 seconds.
  • Example answer: "Created task MSPOutpost-Cleanup: daily at 03:00 as SYSTEM, C:\Windows\System32\cleanmgr.exe"
  • Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.

Inputs

Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.

  • TaskName: Task name, required, no \ / : * ? " < > |. Must start with MSPOutpost- unless AllowAnyName is true.
  • Command: Program to run: a full path or a name found on PATH. Required.
  • Arguments: Arguments passed to the program. Optional.
  • Trigger: Startup (default), Logon, Daily or Once.
  • At: Start time for Daily and Once: HH:mm (24-hour, local) or, for Once only, yyyy-MM-ddTHH:mm. With Once and HH:mm the next such time is used. Ignored for Startup and Logon.
  • RunAs: System (default) or Users (the BUILTIN\Users group, interactive; use with Logon, Daily or Once).
  • Hidden: true to hide the task in Task Scheduler. Default false.
  • AllowAnyName: true to allow a TaskName without the MSPOutpost- prefix. Default false.

Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.

License

Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.

The Script

new-scheduled-task.ps1 · 1056 lines · 54.8 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.