Task Scheduler allows you to automatically run various programs, scripts or commands in Windows according to a schedule, when certain events occur, when the computer is turned on/off, or when the user logs on. The most common way to manage Task Scheduler tasks in Windows is to use the taskschd.msc graphical MMC snap-in. However, you can perform all typical Windows scheduled task management tasks using PowerShell. This article shows you how to use PowerShell to create, modify, activate, deactivate, and delete tasks in the scheduler.
You can manage Windows Scheduler tasks in PowerShell using the built-in ScheduledTasks module. You can list all available cmdlets in the ScheduledTasks module:
Get-Command -Module ScheduledTasks
- Disable-ScheduledTask
- Enable-ScheduledTask
- Export-ScheduledTask
- Get-ClusteredScheduledTask
- Get-ScheduledTask
- Get-ScheduledTaskInfo
- New-ScheduledTask
- New-ScheduledTaskAction
- New-ScheduledTaskPrincipal
- New-ScheduledTaskSettingsSet
- New-ScheduledTaskTrigger
- Register-ClusteredScheduledTask
- Register-ScheduledTask
- Set-ClusteredScheduledTask
- Set-ScheduledTask
- Start-ScheduledTask
- Stop-ScheduledTask
- Unregister-ClusteredScheduledTask
- Unregister-ScheduledTask
Let’s create a simple scheduled task that will restart Windows every day at 8:00 p.m.
The first thing you need to do is create a trigger that defines the time or conditions for the task to start:
$task_trigger= New-ScheduledTaskTrigger -Daily -At 8am
Hint. To specify the start time, use the following parameters on the New-ScheduledTaskTrigger cmdlet:
-
At
-
Daily
-
DaysOfWeek
-
Once
-
Weekly
-
WeeksInterval
-
RandomDelay
-
RepetitionDuration
-
RepetitionInterval
It is also possible to use the following system events in a trigger:
- AtLogOn
- AtStartup
Then you need to specify the action that your scheduler job should perform:
$task_action = New-ScheduledTaskAction -Execute 'shutdown.exe' -Argument ' /r /t 60'
Hint. To run a PowerShell script file (PS1) from the Scheduler, use the following command:
$task_action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument -File C:\PS\myscript.ps1'
You can now create a new scheduler task with the name dailyreboot:
Register-ScheduledTask -TaskName 'dailyreboot' -Action $task_action -Trigger $task_trigger
Open the Task Scheduler graphical console and expand the Task Scheduler Library. You should see your new scheduled task in the list.
To start your scheduled task job manually, run the command:
Start-ScheduledTask –TaskName dailyreboot
Get information about your task:
Get-ScheduledTask dailyreboot
View the status of the last task run and the time of the next run:
Get-ScheduledTask dailyreboot|Get-ScheduledTaskInfo
Show only active scheduled tasks:
Get-ScheduledTask | where state -eq 'Ready'
Disable the scheduler task:
Get-ScheduledTask -taskname dailyreboot | Disable-ScheduledTask
Scheduled tasks that are set to ‘Disabled’ will not be automatically run on the computer.
Enable a scheduled task:
Enable-ScheduledTask-TaskName dailyreboot
Set-ScheduledTask lets you change the options for the scheduler task.
Change task trigger (start time):
$task_trigger = New-ScheduledTaskTrigger -At 2:00PM -Once Set-ScheduledTask -TaskName dailyreboot -Trigger $Task_trigger
Change the action in the task:
$task_action = New-ScheduledTaskAction -Execute 'ping' -Argument '8.8.8.8' Set-ScheduledTask dailyreboot -Action $task_action
By default, a scheduled task runs under the account that created it. In order to run the scheduler task on behalf of the system, you can use the command:
Set-ScheduledTask -TaskName dailyreboot –User "NT AUTHORITY\SYSTEM"
Delete a scheduled task using PowerShell:
Unregister-ScheduledTask -TaskName dailyreboot -Confirm: $false