How to do a schedule job using Windows Power shell with example



How to do a schedule job using Windows Power shell with example

Job scheduling allows you to schedule execution of a Windows 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:

Import-Module PSScheduledJobGet-Job -Name DailyBackup | Receive-Job

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

Start-Job -DefinitionName DailyBackup

Credit : http://powershellmagazine.com

Comments