Often in Windows, tasks are scheduled using the GUI (Graphical User Interface) based “Task Scheduler” app. However, PowerShell can also schedule tasks on Windows using specific cmdlets. Moreover, it can edit, delete, enable, or disable the scheduled tasks. A scheduled task consists of trigger, action and other related settings. When task creation is accomplished, it must also be registered on the system.
This write-up will guide to resolve the mentioned query.
How to Manage and Schedule Tasks in PowerShell?
These methods can be applied to modify the tasks:
- Register a task in PowerShell.
- Unregister a task in PowerShell.
- Enable a task in PowerShell.
- Disable a task in PowerShell.
Method 1: Register a Task in PowerShell
The task in PowerShell can be registered and scheduled using the “Register-ScheduledTask” cmdlet. Below is the code provided to register a task in PowerShell:
$trigger = New-ScheduledTaskTrigger -Daily -At 5am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath "Tasks" -TaskName "NewTask" -Description "Chrome will open at 5am daily."
According to the above code:
- First, create a variable, “$action”, and assign the “New-ScheduledTaskAction” command.
- Alongside it, add the “-Execute” parameter and allocate the “chrome.exe” value to it.
- In the second line, initialize the “$trigger” variable and invoke the “New-ScheduledTaskTrigger” cmdlet.
- After that, add “-Daily” and “-At” parameters. Then, specify the time value as “5am”.
- In the third line, first, add the “Register-ScheduleTask” cmdlet, specify the “-Action” parameter, and assign the “$action” variable to it.
- Add the “-Trigger” parameter and assign the “$trigger” variable.
- Then, specify the “-TaskPath” parameter to add the directory name “Tasks”.
- Also, allocate the name of the task in the “-TaskName” parameter and specify the stated description:
Method 2: Unregister a Task in PowerShell
The task in PowerShell can be unregistered using the “Unregister-ScheduledTask” cmdlet. An overview of the below demonstration to unregister a task in PowerShell is given below:
First, add the “Unregister-ScheduledTask” cmdlet. After that, add the “-TaskName” parameter and specify the task name that needs to be unregistered:
Method 3: Enable a Task in PowerShell
In case the created task is disabled in PowerShell, then the “Enable-ScheduledTask” cmdlet can be used to enable it:
According to the above code:
- First, add the “Get-ScheduledTask” to get the task, then add the “-TaskName” parameter and assign the task name which is “-TaskName”.
- After that, specify the pipeline “|” and add the “Enable-ScheduledTask”:
Method 4: Disable a Task in PowerShell
The tasks in PowerShell can be disabled using the “Disable-ScheduledTask” cmdlet along with the task name:
That was all about managing tasks in PowerShell.
Conclusion
PowerShell uses the “Register-ScheduledTask”, “Unregister-ScheduledTask”, “Enable-ScheduledTask”, or “Disable-ScheduledTask” cmdlet to manage scheduling in Windows effectively. This blog has presented in-depth details to register, unregister, enable, and disable tasks using PowerShell.