Windows PowerShell 4.0 Examples – How to Schedule a Job

Job scheduling allows you to schedule execution of a PowerShell background job for a later time. First thing you do is create a job trigger. This defines when the job will execute. Then you use the Register-ScheduledJob cmdlet to actually register the job on the system. In the following example, every day at 3am we back up our important files:

$trigger = New-JobTrigger -Daily -At 3am
Register-ScheduledJob -Name DailyBackup -Trigger $trigger -ScriptBlock {Copy-Item c:\ImportantFiles d:\Backup$((Get-Date).ToFileTime()) -Recurse -Force -PassThru}

Once the trigger has fired and the job has run, you can work with it the same way you do with regular background jobs:

Get-Job -Name DailyBackup | Receive-Job

You can start a scheduled job manually, rather than waiting for the trigger to fire:

Start-Job -DefinitionName DailyBackup

The RunNow parameter for Register-ScheduledJob and Set-ScheduledJob eliminates the need to set an immediate start date and time for jobs by using the -Trigger parameter:

Register-ScheduledJob -Name Backup -ScriptBlock {Copy-Item c:\ImportantFiles d:\Backup$((Get-Date).ToFileTime()) -Recurse -Force -PassThru} -RunNowIn

About the author

Lima is the visual nautical indicator for "stop instantly."

Leave a Reply