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:
$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:
$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.