Task Scheduler is a Windows tool that allows you to Create and automate any routine tasks on a chosen computer. Task Scheduler is mainly used to run any monitoring tools, and for maintenance tasks such as disk defragmentation, disk Clean-up, and to install windows updates. Also, you can use the Task Scheduler to execute tasks such as starting an application, sending an email message, run commands, to execute scripts at a particular day and time, or showing a message box.
Task Scheduler can be scheduled in response to the following events or triggers.
At a Specific Time.
At a specific time on a daily schedule
At a specific time on a weekly schedule
At a specific time on a monthly schedule
At any Specific Time.
When the system is booted.
When the computer enters an idle state.
When a user Logs on.
When the task is registered.
You can create tasks and schedule based on the above response. This article will guide you to create Scheduled task using PowerShell.
Create Scheduled task using PowerShell:
To create a scheduled task using PowerShell, we need two main variables, $action, and $trigger. Action variable will store the action and application location. And the Trigger variable will store the timings of the scheduled task.
Open PowerShell in elevated mode and type the following command to create a $action variable.
$action = New-ScheduledTaskAction -Execute 'application-path'
Replace the application-path with your actual path of the app you want to run.
For Example if you want to execute any application to you need to replace the application-path with the actual one. Lets take google chrome, right click on Google chrome shortcut and choose open file location, Now copy the path from the address bar and replace it with the application-path. so your command will look like this.
$action = New-ScheduledTaskAction -Execute 'C:\Program Files\Google\Chrome\Application\chrome.exe'
If you want to open any PowerShell script then you need to use –argument command and specify the script path. The command will look like this
New-ScheduledTaskAction -Execute 'powershell.exe' -argument script-path
Next, you need to create a trigger variable, using this variable we need to set the time when you want to execute the task. Use the following command to create a Trigger variable and set the time.
$trigger = New-ScheduledTaskTrigger -Once -At 08:30PM
This command will execute the task once at 8.30 PM on a particular date. You can also use various settings like –Daily, -Weekly, -Monthly, etc. Also, you can choose the time in 12 or 24-hour format. If you are choosing the 12-hour format then you need to define the AM or PM.
Once you created the variables successfully, then you need to enter the following command to Schedule a Task.
Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath "SeTName" -TaskName "SetDescription" -Description "DemoDescription"
Modify the SetName, SetDescription and DemoDescription according to your Preference.
Once you done all the changes hit enter now you will get a successful message with TaskName state and Taskpath.