fbpx

Master Powershell Scheduled Tasks in Minutes: The Ultimate Guide

With PowerShell, you can easily automate various tasks and scripts by creating and managing scheduled tasks. By using PowerShell to manage your scheduled tasks, you can save time and ensure that your tasks are executed consistently and reliably.

In this task, we will explore the different methods for creating and managing a PowerShell scheduled task, including the use of cmdlets and the Windows Task Scheduler GUI, as well as best practices and troubleshooting tips.

System Requirements to Make a PowerShell Scheduled Task

There are three essentials you will need if you are trying to create a scheduled task in PowerShell – 

  1. A PC running on Windows 10 or a newer version of the Operating System (OS).
  2. Windows PowerShell; any version after version 3.0 can be used for this
  3. A text line editor; We recommend using Visual Studio Code or NotePad++.

Making a New Scheduled Task

In PowerShell, when creating a scheduled task, you have the ability to specify several key elements: the action, trigger, principal, and settings.

  • The action specifies what should happen when the task runs, such as running a PowerShell script, launching a program, or sending an email.
  • The trigger determines when the task should be executed, such as at a specific time, when the computer starts, or when a specific event occurs.
  • The principal defines the security context under which the task should run, such as a specific user account or the system account.
  • The settings allow you to specify additional options for the task, such as whether to stop the task if it runs for too long, whether to run the task even if the user is not logged on, and more.

Making a Task Action

To create a task action in PowerShell, use the New-ScheduledTaskAction cmdlet to specify the action type and any additional settings. For example, to run a PowerShell script, use the -Execute parameter followed by the path to the script file. You can also include arguments or specify the working directory. Here is an example:

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\backup.ps1" -WorkingDirectory "C:\Scripts"
Making a Task Action

Once the task action is defined, use it as a parameter when creating the scheduled task with New-ScheduledTaskTrigger and Register-ScheduledTask.

Including a Trigger

In PowerShell, a scheduled task trigger specifies when the task should run. You can add a trigger to a scheduled task using the New-ScheduledTaskTrigger cmdlet, which allows you to specify the trigger type and associated settings.

For example, if you want to run the task daily at a specific time, you can use the -Daily parameter followed by the -At parameter to specify the time of day. You can also specify additional settings for the trigger, such as the start time, end time, and repetition interval.

Once the trigger is defined, you can use it as a parameter when creating the scheduled task with the Register-ScheduledTask cmdlet. Here is an example of how to add a trigger to a scheduled task:

$trigger = New-ScheduledTaskTrigger -Daily -At "6:00 AM" Register-ScheduledTask -TaskName "MyTask" -Action $action -Trigger $trigger -User "DOMAIN\UserName" -Password "MyPassword"
Including a Trigger

This creates a trigger that runs the task daily at 6:00 AM and registers the task with the name “MyTask”, using the previously defined action and the specified user credentials.

Task Registration

After defining the task action and trigger, the final step is to register the task using the Register-ScheduledTask cmdlet. This cmdlet takes a number of parameters that allow you to specify various settings for the scheduled task, such as the task name, user credentials, and security options.

For example, to register a scheduled task with the name “MyTask” and user credentials “DOMAIN\UserName” and “MyPassword”, you can use the following command:

Register-ScheduledTask -TaskName "MyTask" -Action $action -Trigger $trigger -User "DOMAIN\UserName" -Password "MyPassword"

This command registers the scheduled task with the specified action and trigger and sets the user credentials to run the task. Once the task is registered, it will appear in the Task Scheduler interface and can be managed like any other scheduled task.

Executing your Task

Once a scheduled task has been created and registered using PowerShell, it will automatically run according to the defined trigger. However, you may also want to manually run the task outside of its scheduled time or verify that it is running as expected.

To manually run a scheduled task in PowerShell, you can use the Start-ScheduledTask cmdlet. This cmdlet allows you to specify the name of the task and any additional parameters that may be required by the task itself.

For example, to manually start the task “MyTask”, you can use the following command:

Start-ScheduledTask -TaskName "MyTask"

This command starts the specified task and executes its defined action. You can also use the -Verbose parameter to display detailed information about the task’s execution.

Managing a Pre-Existing Scheduled Task

Managing your User Account, Security Options, and Compatibility Settings

When creating and managing scheduled tasks in PowerShell, it is important to consider various user account, security, and compatibility settings that may affect the task’s behavior.

For user account settings, you can specify which user account the task should run under and whether or not to store the user’s password securely. You can also specify whether the task should run whether the user is logged on or not, and whether to run with the highest privileges.

For security settings, you can specify whether the task should be run with a restricted token and which security principal the task should be run as. You can also specify the level of security required for the task, such as running in a restricted mode or with enhanced security.

For compatibility settings, you can specify which version of PowerShell to use when running the task, and whether the task should be run in 32-bit mode or 64-bit mode.

All of these settings can be configured using the Register-ScheduledTask cmdlet when creating or updating a scheduled task. For example, to create a scheduled task that runs under the user account “DOMAIN\UserName” with the highest privileges and requires enhanced security, you can use the following command:

Register-ScheduledTask -TaskName "MyTask" -Action $action -Trigger $trigger -User "DOMAIN\UserName" -RunLevel Highest -SecurityDescriptor $secDesc
Managing your User Account, Security Options, and Compatibility Settings

This command registers the scheduled task with the specified action and trigger, sets the user account to run under, and configures the task to run with the highest privileges and enhanced security.

Adjusting the Task Trigger

To modify the trigger of an existing scheduled task, you first need to retrieve the task object using the Get-ScheduledTask cmdlet. This will return a ScheduledTask object that you can then modify using the Set-ScheduledTaskTrigger cmdlet.

For example, to change the trigger of a scheduled task named “MyTask” to run daily at 3:00 PM, you can use the following commands:

$task = Get-ScheduledTask -TaskName "MyTask" $trigger = New-ScheduledTaskTrigger -Daily -At 3:00pm Set-ScheduledTaskTrigger -Task $task -Trigger $trigger
Adjusting the Task Trigger

This retrieves the task object for “MyTask”, creates a new daily trigger that runs at 3:00 PM using the New-ScheduledTaskTrigger cmdlet, and then sets this new trigger using the Set-ScheduledTaskTrigger cmdlet.

You can also modify other properties of the trigger, such as the start time, end time, and repetition interval, by using the appropriate parameters with the New-ScheduledTaskTrigger cmdlet.

Deleting a Scheduled Task

Making a Scheduled Task Backup

Backing up a scheduled task in PowerShell involves exporting its settings to an XML file. This can be useful if you want to create a backup of a task’s configuration or transfer it to another computer.

To export a scheduled task, you can use the Export-ScheduledTask cmdlet. For example, to export a task named “MyTask” to a file named “MyTask.xml” in the current directory, you can use the following command:

Export-ScheduledTask -TaskName "MyTask" -XmlFilePath ".\MyTask.xml"
Making a Scheduled Task Backup

This will export the task’s settings to an XML file in the current directory.

To import a task from an XML file, you can use the Register-ScheduledTask cmdlet with the -Xml parameter. For example, to import a task from a file named “MyTask.xml”, you can use the following command:

Register-ScheduledTask -Xml (Get-Content ".\MyTask.xml" | Out-String) -TaskName "MyTaskCopy"
Making a Scheduled Task Backup

This will import the task’s settings from the XML file and create a new task named “MyTaskCopy.

Overall, backing up a scheduled task by exporting its settings to an XML file can be a useful way to ensure that you have a copy of the task’s configuration in case it needs to be restored or transferred to another computer.

Removing a Scheduled Task

Deleting a scheduled task in PowerShell is a straightforward process that can be done quickly and easily. To delete a scheduled task in PowerShell, you can use the Unregister-ScheduledTask cmdlet. The syntax is simple:

Unregister-ScheduledTask -TaskName "TaskName"

Replace “TaskName” with the name of the task that you want to delete.

Restoring a deleted Scheduled Task

If you have accidentally deleted a scheduled task, you can restore it by importing a backup of the task.

To restore a deleted scheduled task in PowerShell, you can use the Import-ScheduledTask cmdlet. The syntax is:

Import-ScheduledTask -TaskName "TaskName" -Xml "C:\path\to\backup\TaskName.xml"

Replace “TaskName” with the name of the task that you want to restore, and “C:\path\to\backup\TaskName.xml” with the path to the backup XML file.

Once you have imported the scheduled task, you should be able to see it in the Task Scheduler library and manage it as you normally would.

By now, you should have been able to schedule the task you want on your PC. For further information, you can visit the scheduled task page on the Microsoft PowerShell webpage.