Powershell

How to Use the New-ScheduledTask Cmdlet in PowerShell?

Scheduling tasks on Windows is always beneficial as it increases productivity. Tasks can be scheduled on Windows using the “Task Scheduler” app. However, PowerShell can also perform the stated operation. It uses the “New-ScheduledTask” cmdlet to create a scheduled task instance. Scheduling of tasks is preferred in PowerShell over the GUI-based “Task Scheduler” app, because of its interaction ability with the system through a script.

This guide will reveal the details related to PowerShell’s “New-ScheduledTask” cmdlet.

How to Use the New-ScheduledTask Cmdlet in PowerShell?

The “New-ScheduledTask” cmdlet does not register the tasks automatically using the task scheduler service. To learn more about the usage of the “New-ScheduledTask” cmdlet, let’s check out the given set of examples.

Example 1: Use the “New-ScheduledTask” Cmdlet to Create a New Scheduled Task to Start a Process Every Day at 5:00 PM
Execute the below-given code to schedule a new task in PowerShell:

$Action = New-ScheduledTaskAction -Execute "notepad.exe"
$Trigger = New-ScheduledTaskTrigger -Daily -At 05:00PM
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskPath "Schedule" -TaskName "StartNotepad" -Description "Notepad will launch at 5 pm daily."

According to the above code:

  • First, initialize a variable, “$Action”, and assign it the cmdlet “New-ScheduledTaskAction”.
  • Then, use the “-Execute” parameter and specify the “notepad.exe” value.
  • Next, initialize another variable “$Trigger” and assign it the “New-ScheduledTaskTrigger” cmdlet.
  • After that, add “-Daily” and “-At” parameters. Then, specify the time “05:00PM” to the “-At” parameter.
  • In the third line, use the cmdlet “Register-ScheduleTask”, along with the “-Action” parameter, and assign it the variable “$Action”.
  • Next, specify the parameter “-Trigger” and assign it the variable “$Trigger”.
  • Moving on, use the parameter “-TaskPath” to add the directory name “Schedule”.
  • Lastly, provide the task name to the “-TaskName” parameter and specify the description to the “-Description” parameter:

Example 2: Use the “New-ScheduledTask” Cmdlet to Create a New Scheduled Task to Run the Script File When the Computer Starts
Specify the script file address to the “-Execute” parameter in the given code to run a script at the start of a computer:

$Action = New-ScheduledTaskAction -Execute "C:\Docs\New.ps1"
$Trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "Script"

That’s all! The “New-ScheduledTask” cmdlet has been demonstrated in detail.

Conclusion

PowerShell’s “New-ScheduledTask” cmdlet is responsible for creating scheduled task instances. It can create multiple instances at once. In this particular post, the “New-ScheduledTask” cmdlet has been comprehensively illustrated in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.