#requires -version 3.0 #Demonstrate scheduled jobs in PowerShell 3.0 #This must be run in an elevated session #Don't confuse this with ScheduledTasks module in Windows 8 #THIS IS NOT A SCRIPT, ONLY A LISTING OF COMMANDS TO RUN INDIVIDUALLY # These commands are in the PSScheduledJob module. # importing a module is optional in v3. get-command -module PSScheduledJob # Define when to run the job help new-jobTrigger $trigger = New-JobTrigger -Once -At (Get-Date).AddMinutes(30) # Let's see what we created $trigger # Define a name for the scheduled job to save typing $name="Demo Scheduled Job" # Define a scriptblock. You would normally create a multiline scriptblock. # I have a one liner for the sake of my demo. $action={$p=get-process | where {$_.ws -gt 10mb} | sort WS -descending ; write $p ; $p | export-clixml -path c:\work\proc10mb.xml} # Register the job help Register-ScheduledJob Register-ScheduledJob -Name $name -ScriptBlock $action -Trigger $trigger # We can see the scheduled job is now registered Get-ScheduledJob -Name $name # Define a variable for the job path so we don't have to retype $jobpath= "$env:LocalAppData\Microsoft\Windows\PowerShell\ScheduledJobs" dir $jobpath -recurse # show the job in Task Scheduler Microsoft\Windows\PowerShell\SchedJobs Taskschd.msc # this is a rich object Get-ScheduledJob -Name $name | get-member Get-ScheduledJob -Name $name | Select * # We can also see when it will execute get-jobtrigger $name #or this way Get-ScheduledJob $name | Get-JobTrigger # let's look at options Get-ScheduledJobOption -Name $name # or modify options. Works best in a pipeline help Set-ScheduledJobOption Get-ScheduledJobOption -Name $name | Set-ScheduledJobOption -RunElevated -passthru | Select Run* # we can modify the trigger help set-jobtrigger Get-JobTrigger $Name Get-JobTrigger $Name | Set-JobTrigger -DaysOfWeek Thursday -Weekly -at 12:00PM -PassThru | Select * # get the job using the standard job cmdlets after it has run Get-Job Get-ScheduledJob # You can start a scheduled job manually. Notice the new parameter Start-Job -DefinitionName $Name # Now what do we see in the job queue? Get-Job # there are some new properties Get-Job -Name $name | Select * # get job results Receive-job $name -keep #results written to disk dir $jobpath -recurse #disabling the job Help Disabled-ScheduledJob Disable-ScheduledJob $name -WhatIf Disable-ScheduledJob $name -PassThru #if I wanted to re-enable Enable-ScheduledJob $name -WhatIf #And finally we'll remove the scheduled job help Unregister-ScheduledJob Unregister-ScheduledJob -Name $name Get-ScheduledJob #also clears job queue Get-job #UNREGISTERING ALSO DELETES HISTORY AND OUTPUT dir $jobpath -recurse #again, these are the commands you'll use get-command -module PSScheduledJob #read help help about_scheduled_jobs cls