Windows PowerShell 4.0 Examples – How to Schedule a Job

Last Updated on

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:

1
2
$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:

1
Get-Job -Name DailyBackup | Receive-Job

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

1
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:

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

About the author

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

Leave a Reply

Welcome!

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.