Scheduler cmdlets - PowerShell


Overview

Scheduler cmdlets manage scheduled tasks in Windows PowerShell. They allow you to create, modify, delete, and query scheduled tasks.

Syntax

Get-ScheduledTask

Get-ScheduledTask [-TaskName] <String[]> [-TaskPath] <String> [-XML] [-TaskCategory] <String> [-ErrorAction] <ActionPreference> [-WarningAction] <ActionPreference> [-Verbose] [-Debug] [-Confirm] [-WhatIf] [-OutVariable] <String>

Register-ScheduledTask

Register-ScheduledTask [-TaskName] <String> [-TaskPath] <String> [-Xml] <String> [-Force] [-Confirm] [-WhatIf] [-OutVariable] <String>

Unregister-ScheduledTask

Unregister-ScheduledTask [-TaskName] <String[]> [-TaskPath] <String> [-Confirm] [-WhatIf] [-OutVariable] <String>

Start-ScheduledTask

Start-ScheduledTask [-TaskName] <String[]> [-TaskPath] <String> [-Confirm] [-WhatIf] [-OutVariable] <String>

Stop-ScheduledTask

Stop-ScheduledTask [-TaskName] <String[]> [-TaskPath] <String> [-Force] [-Confirm] [-WhatIf] [-OutVariable] <String>

Options/Flags

Get-ScheduledTask

  • -TaskName: Specifies the name of the scheduled task to retrieve.
  • -TaskPath: Specifies the path to the scheduled task to retrieve.
  • -XML: Retrieves the scheduled task in XML format.
  • -TaskCategory: Retrieves scheduled tasks that belong to the specified category.
  • -ErrorAction: Specifies the action to take if an error occurs.
  • -WarningAction: Specifies the action to take if a warning occurs.
  • -Verbose: Outputs additional detailed information.
  • -Debug: Outputs debugging information.
  • -Confirm: Prompts for confirmation before performing the operation.
  • -WhatIf: Performs a simulation of the operation without actually executing it.
  • -OutVariable: Stores the output of the command in the specified variable.

Register-ScheduledTask

  • -TaskName: Specifies the name of the scheduled task to register.
  • -TaskPath: Specifies the path to the scheduled task to register.
  • -Xml: Specifies the XML definition of the scheduled task to register.
  • -Force: Overwrites an existing scheduled task with the same name.
  • -Confirm: Prompts for confirmation before performing the operation.
  • -WhatIf: Performs a simulation of the operation without actually executing it.
  • -OutVariable: Stores the output of the command in the specified variable.

Unregister-ScheduledTask

  • -TaskName: Specifies the name of the scheduled task to unregister.
  • -TaskPath: Specifies the path to the scheduled task to unregister.
  • -Confirm: Prompts for confirmation before performing the operation.
  • -WhatIf: Performs a simulation of the operation without actually executing it.
  • -OutVariable: Stores the output of the command in the specified variable.

Start-ScheduledTask

  • -TaskName: Specifies the name of the scheduled task to start.
  • -TaskPath: Specifies the path to the scheduled task to start.
  • -Confirm: Prompts for confirmation before performing the operation.
  • -WhatIf: Performs a simulation of the operation without actually executing it.
  • -OutVariable: Stores the output of the command in the specified variable.

Stop-ScheduledTask

  • -TaskName: Specifies the name of the scheduled task to stop.
  • -TaskPath: Specifies the path to the scheduled task to stop.
  • -Force: Forces the scheduled task to stop, even if it is running.
  • -Confirm: Prompts for confirmation before performing the operation.
  • -WhatIf: Performs a simulation of the operation without actually executing it.
  • -OutVariable: Stores the output of the command in the specified variable.

Examples

Get a scheduled task by name

Get-ScheduledTask -TaskName "MyTask"

Register a scheduled task from an XML file

Register-ScheduledTask -TaskName "MyTask" -Xml (Get-Content "C:\path\to\task.xml")

Unregister a scheduled task by path

Unregister-ScheduledTask -TaskPath "\Microsoft\Windows\DiskCleanup\SilentCleanup"

Start a scheduled task

Start-ScheduledTask -TaskName "MyTask"

Stop a scheduled task

Stop-ScheduledTask -TaskName "MyTask"

Common Issues

Task not found

If you receive an error message stating that the task cannot be found, verify that the task name or path is correct.

Invalid XML

If you encounter an error when registering a scheduled task from an XML file, ensure that the XML is well-formed and does not contain any syntax errors.

Access denied

If you are unable to perform a scheduled task operation, make sure that you have sufficient permissions to do so.

Integration

Scheduler cmdlets can be used in conjunction with other PowerShell commands to automate tasks. For example, you can use the Get-ScheduledTask cmdlet to retrieve a scheduled task and then use the Invoke-Command cmdlet to run the task.

  • New-ScheduledTask: Creates a new scheduled task.
  • Set-ScheduledTask: Modifies an existing scheduled task.
  • Disable-ScheduledTask: Disables an existing scheduled task.
  • Enable-ScheduledTask: Enables a disabled scheduled task.