PowerShell Scheduler cmdlets

A Scheduled Job is a background job that can be started automatically on a one-time or recurring schedule.

   Enable-ScheduledJob    Enable a scheduled job
  Disable-ScheduledJob    Disable a scheduled job
      Get-ScheduledJob    Get scheduled jobs on the local computer
      Set-ScheduledJob    Change the properties of a scheduled job.
  Register-ScheduledJob   Create a new scheduled job
  Unregister-ScheduledJob Delete a scheduled job

  Get-ScheduledJobOption  Get the options of scheduled job
  New-ScheduledJobOption  Create new options for a scheduled job
  Set-ScheduledJobOption  Change the options of a scheduled job

     Disable-ScheduledTask   Disable a scheduled task.
      Enable-ScheduledTask   Enable a scheduled task.
      Export-ScheduledTask   Export a scheduled task as an XML string.
         Get-ScheduledTask   Get the task definition object for a registered task.
         New-ScheduledTask   Create a scheduled task instance.
         Set-ScheduledTask   Modify a scheduled task.
       Start-ScheduledTask   Start one or more instances of a scheduled task.
        Stop-ScheduledTask   Stop all running instances of a task.
    Register-ScheduledTask   Register a scheduled task.
  Unregister-ScheduledTask   Unregister a scheduled task.
New-ScheduledTaskPrincipal   Create an object containing a scheduled task principal.
    New-ScheduledTaskAction  Create a scheduled task action.
New-ScheduledTaskSettingsSet Create a new scheduled task settings object.
     Get-ScheduledTaskInfo   Get run-time information for a scheduled task.
  New-ScheduledTaskTrigger   Create a scheduled task trigger object.

     Get-ClusteredScheduledTask    Get clustered scheduled tasks for a failover cluster.
Register-ClusteredScheduledTask    Register a scheduled task on a failover cluster.
     Set-ClusteredScheduledTask    Change settings for a clustered scheduled task.
Unregister-ClusteredScheduledTask  Remove a scheduled task from a failover cluster.

  Add-JobTrigger     Add  a job trigger to a scheduled job
  Get-JobTrigger     Get the job triggers of scheduled jobs
  New-JobTrigger     Create a new job trigger
  Set-JobTrigger     Change the job trigger of a scheduled job

The cmdlets on this page are available in PowerShell 3.0 and greater.

When scheduling a single job you may find it easier to just call the old school SCHTASKS command, for multiple jobs on multiple machines the PowerShell cmdlets will start to show their advantages.
Whichever method is used, the same Scheduled Job will result.

A Scheduled Job can be exported from one computer as an .XML file and imported to a different machine, this can be a good option if you want consistent job settings on multiple machines without having to write any scripts.

Set the owner of a Scheduled Job

There are two main ways to run a scheduled job:

For a group - the scheduled job will run as the logged in user if they are a member of the named group.
Most often this is the group BUILTIN/USERS, every domain user is a member of USERS, so the job will run for the currently logged in user. The scheduled job will thus have access to the same resources as that User Account (file permissions, network access etc.) If no-one is logged in the job will not run.

For a named user, either a local account or a Domain Account - the scheduled job will have access to the same resources as that Account (file permissions, network access etc)

If it is possible to run a job as the already logged-in user, or a local account this will avoid the network traffic of a domain login, which can be significant if hundreds or thousands of workstations all run a scheduled job at the same time.

Service Accounts

The LocalSystem account (NT AUTHORITY\SYSTEM + BUILTIN\Administrators) is a highly privileged account with access to the network and Active Directory. It does not have a user profile and does not allow interactive logins (interacting with the desktop.) While the SYSTEM account was used to run System Services and Scheduled jobs in early versions of Windows NT, this is no longer recommended.

The LocalService account (NT_AUTHORITY\LocalService), is a limited service account that is meant to run standard least-privileged services, it has no ability to access the network as the machine.

The NetworkService account (NT AUTHORITY\NetworkService) is meant to run standard least-privileged services. but still has the right to access the network as the machine.

Both LocalService and NetworkService have minimal authority on the local computer, basically the same authority as an unprivileged user account plus the Logon as a service right, which any service account requires. Because these built-in accounts run multiple services, any additional rights granted to them would pass on to those processes too.
To avoid this, use a dedicated user account (or Managed Service Account) with the desired permissions.

Job Options: Run only when user is logged on

When this option is chosen the running job may be visible to the user, if the same job is set to Run whether user is logged on or not, it will be invisible to any user who happens to be logged on to the machine.

Job Options: Wake the computer to run this task

To wake up, Windows must talk to the BIOS to schedule a wake up. For this to work, you must enable the"Allow wake timers" setting on the current power policy ( Control Panel ➞ Power Options ). Support for this is limited, for example a BIOS written to ACPI v4 will not accept a wake schedule more than 30 days into the future.

This property is essential if you need the task to run on a machine that has PowerSaving enabled.
See also Powercfg.exe -WakeTimers

Job Options: Run task as soon as possible after a scheduled start is missed

This option does not run a missed job immediately, which you might reasonably expect. There is a 10-minute delay. So if a job was missed because the PC was off, it will run exactly 10 minutes after the computer is powered back on.
If multiple jobs are missed, they will all start at the same time: exactly 10 minutes after the computer is powered back on.

This delayed start is not shown in under 'Next Run Time', but will appear under 'Last Run Time' once the job has run.
This option is ignored completely if the job has a "ONE Time" trigger.

Notes

When a scheduled job is running, the environment variable %SESSIONNAME% is not populated:
if ($env:SessionName -eq $null) {Echo 'This is a scheduled job'}

If a Scheduled Job has multiple 'Actions' they will run in order, if one stalls, it will block later tasks from running. If you need tasks to run asynchronously, set them up as separate jobs.

The options described on this page are for the Windows Scheduler 2.0 available in Windows Vista and greater.

Windows 8

The two actions: showMessage and sendEmail are deprecated (use IExecAction with Send-MailMessage as a workaround).

“In order to plan your future wisely, it is necessary that you understand and appreciate your past” ~ Jo Coudert

Related

Task Scheduler error codes
SCHTASKS - Create /Edit a Scheduled Job.
Run with elevated permissions


 
Copyright © 1999-2024 SS64.com
Some rights reserved