How to Create Scheduled Tasks in Windows PowerShell (and Why You Should)
Oftentimes, when you want to schedule tasks in Windows, you’ll usually reach for the Windows Task Scheduler utility first. However, it is possible to use Windows PowerShell cmdlets to create, edit and delete scheduled tasks.
But why should you use PowerShell to schedule tasks instead of the designated Tash Scheduler tool? Let’s find out.
Why Use PowerShell Instead of Task Scheduler?
For most Windows users, it is easier to use the Task Scheduler to create simple scheduled tasks. However, the main reason to use PowerShell instead of Task Scheduler is mainly the ability to create a script that interacts with the system through a scheduled task.
The Task Scheduler has been part of Windows for a long time, and you can learn more about running programs automatically using the Task Scheduler in our guide. This will help you to decide the best method for creating scheduled tasks for your specific needs.
What Is a PowerShell Script?
Windows PowerShell is a task automation and configuration management program. It also includes a scripting language. PowerShell scripts, called “cmdlets,” allow you to create complex, multi-action operations.
A PowerShell script could be used for something as simple as collecting and presenting information about a computer. A network administrator could create a script that shuts down every computer on a network that isn’t being used.
Scripts are most commonly used by both system and network administrators, but that doesn’t mean you can’t create PowerShell scripts to perform simple and complex actions on your personal computer. In fact, there are a lot of simple Windows scripts that are easy to set up, yet provide a lot of functionality.
Creating a Scheduled Task in PowerShell (Elevated)
You will need to open PowerShell with administrator privileges when you want to create scheduled tasks. If you don’t know how to do this, be sure to learn more about starting programs with elevated privileges.
To open PowerShell, right-click the Start Menu button and select PowerShell (Admin) from the power user menu. If you don’t see that option, search for PowerShell in Windows Search and select Run as Administrator.
You need to create a variable to contain the task action. To do this, type: $action = New-ScheduledTaskAction -Execute ‘PROGRAM‘ and press Enter. Replace PROGRAM with the name of the program you are creating the task for.
The variable name in the command above is the $action part. You can change this to anything you want, but it is best to keep it short and descriptive. It also needs to be in lowercase.
Next, create the trigger for the scheduled task. This will include a time and a repeat frequency. To add a trigger, type: $trigger = New-ScheduledTaskTrigger –SETTING -At TIME. Replace -SETTING with the frequency, such as Daily. Replace -At TIME with a time, such as “At 7am.”
You can use Once, Daily, Weekly, or Monthly for the frequency. The time can be in 12 or 24-hour format. If you are using the Weekly frequency, you can also add -DaysOfWeek, and then the day. Tuesday, for example. You can also use -DaysInterval to control the frequency. For example, –DaysInterval 3, will run the task every three days.
You now need to put all the information together into a single command. In the following command line, replace TASK-FOLDER, TASK-NAME, and OPTIONAL-DESCRIPTION-TEXT with your task information. The -TaskPath is optional but helps you to identify your created tasks.
To create the scheduled task, type: Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath “TASK-FOLDER” -TaskName “TASK-NAME” -Description “OPTIONAL-DESCRIPTION-TEXT“. Press Enter.
The scheduled task will then be created and will activate when the frequency and time triggers are reached.
Modify a Scheduled Task in PowerShell (Elevated)
You can also use PowerShell to modify a scheduled task created previously. You can edit a scheduled task even if it is currently active. You will need to know the name of the scheduled task and the task path.
Open PowerShell with administrator privileges by searching in Windows Search and clicking Run as Administrator.
You need to create a variable to hold the scheduled task changes. Type: $trigger = New-ScheduledTaskTrigger –SETTING –At TIME. Change -SETTING and -At TIME to the frequency and time you want to set for the task.
To change the program the scheduled task uses, type: $action = New-ScheduledTaskAction -Execute ‘PROGRAM’. Replace PROGRAM in the command with the name of the program you want to use along with the path to the .exe file.
Next, apply those changes to the scheduled task by typing: Set-ScheduledTask -Trigger $trigger -Action $action -TaskPath “TASK-FOLDER” -TaskName “TASK-NAME“. Replace TASK-FOLDER and TASK-NAME with details from the task you want to change.
How to Delete Scheduled Tasks in PowerShell
Scheduled tasks created in PowerShell can also be deleted in PowerShell.
Open PowerShell (Elevated) and type the following command to confirm the task exists and is active: Get-ScheduledTask -TaskName “TASK-NAME”. Replace TASK-NAME with the name of the task you want to delete.
Assuming the scheduled task is found, you can delete it with the following command: Unregister-ScheduledTask -TaskName “TASK-NAME” -Confirm:$false. Replace TASK-NAME with the name of the task you want to delete.
To confirm that the task has been deleted, type: Get-ScheduledTask -TaskName “TASK-NAME“, replacing TASK-NAME with the name of the scheduled task you deleted.
You should then see a warning that no task exists with that name. If you see details of the queried scheduled task, it was not deleted correctly. Check the details you entered during step 1 above.
Additional PowerShell Cmdlets for Scheduled Tasks
Now you understand the basics of creating scheduled tasks in PowerShell, you can experiment with other cmdlets.
There are several other ways to control scheduled tasks with command lines. These include temporarily disabling tasks, exporting a scheduled task as an XML string, and stopping all running tasks.
You can find a complete list of all PowerShell scheduled task cmdlets on Microsoft Docs.
Creating Scheduled Tasks in Windows PowerShell
Not everyone will need to use PowerShell for creating scheduled tasks. The Task Scheduler does a fine job of letting you schedule simple tasks such as opening apps. But if you need to know how to create scheduled tasks in PowerShell, the powerful Windows automation tool, this guide is a great place to start.
Read the full article here