Managing Windows Scheduled Tasks with Windows PowerShell
List, create and update Windows Scheduled Tasks with PowerShell
Microsoft Windows Task Scheduler component is a standard part of Microsoft Windows operating system since Windows98. It allows schedulex execution of programs or scripts at specific pre-defined time or recurrent time interval.
Repetitive creating and updating Windows Scheduled Tasks manually using Windows built in manager can be annoying. That is where PowerShell comes into play with it's out of the box Scheduled Task module.
Scheduled Task module is integrated art of PowerShell 3 which is shipped starting with Windows 8 and Windows Server 2012. In case you have older version of Windows operating system, please consider updating Windows Management Framework by downloading from Microsoft Download Center
Windows Management Framework 3
https://www.microsoft.com/en-us/download/details.aspx?id=34595
/> or
Windows Management Framework 5
https://www.microsoft.com/en-us/download/details.aspx?id=50395
Before we process with Scheduled Tasks we need to mention that each task has two core properties which are schedule and executable. Schedule represents the trigger when the executable will be started.
Creating new Scheduled Task
As we mentioned we need to have executable for the scheduled task. For the testing purpose I created a simple PowerShell script that just takes the folder and creates backup of it by creating zip archive of the folder content. I named this script file Backup-Folder.ps1 and saved it on folder C:\Scripts. The content of this file is following:
Param( [Parameter(Mandatory=$true)] [string]$backupFolder, [Parameter(Mandatory=$true)] [string]$destinationFolder ) $outputArchive = $destinationFolder + (Get-Date -format "yyyyMMddHHmmss") + ".zip" Compress-Archive -Path $backupFolder -DestinationPath $outputArchive -CompressionLevel Optimal Write-Output $outputArchive
Now we need to create a schedule which will backup folder passed as parameter to location that we also pass as a second parameter. Let's say this backup needs to be triggered every day before midnight at 23:59.
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-command C:\Scripts\Backup-Folder.ps1 -backupFolder C:\temp -destinationFolder C:\\' $trigger = New-ScheduledTaskTrigger -Daily -At 23:59pm $prncipal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest Register-ScheduledTask -Action $action -Trigger $trigger -Principal $prncipal -TaskName "\PowerShell\Daily folder backup" -Description "Backup folder at the end of the day"
If you open Scheduled Task manager now you will see that there is section PowerShell added to the left navigation pane and in it your Scheduled Task is created. You can run it from the right click context menu and check for zip archive created to make sure the task is working properly.
You can notice that we also set permissions to run as Administrator so that you do not face any security exceptions during run, but this property needs to be set accordingly to your needs. No need to give more permissions that it is required by the executable and operation that it performs.
Listing existing Scheduled Tasks
You can get Schedulet Task object by simple Get-ScheduledTask methods call.
Get-ScheduledTask -TaskPath "\PowerShell\"
TaskPath TaskName State
-------- -------- -----
\PowerShell\ Daily folder backup Ready
Depending whether you call Get-ScheduledTask with -TaskPath parameter, or you call it with -TaskName parameter you will get collection of tasks or a single task object
Get-ScheduledTask -TaskName "Daily folder backup"
TaskPath TaskName State
-------- -------- -----
\PowerShell\ Daily folder backup Ready
Updating exisiting Scheduled Task
Two most frequent operations you will do with existing Scheduled Tasks is disabling and enabling them and deleting them
Disable/Enable scheduled task
If you want to disable specific task, simply call method Disable-ScheduledTask with -TaskName paremeter
Disable-ScheduledTask -TaskName "Daily folder backup"
In case you want to disable group of tasks, combine Get-ScheduledTask with Disable-ScheduledTask
Get-ScheduledTask -TaskPath "\PowerShell" | Disable-ScheduledTask
Same operations on single or multiple tasks can be done for enabling with Enable-ScheduledTask method
Removing Scheduled Task from the registry
Unregister-ScheduledTask -TaskName "Daily folder backup"
If you want to check your scheduled task from the Windows Task Manager UI, you can run taskschd.msc from Windows Run window or access it from Control Panel/Administartive Tools/Task Scheduler
References
- https://en.wikipedia.org/wiki/Windows_Task_Scheduler
- https://docs.microsoft.com/en-us/powershell/module/psscheduledjob/?view=powershell-5.1
Disclaimer
Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
Comments for this article