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 |