CmdID / PowerShell / Cmdlets / Set
Set-ScheduledJob
Changes scheduled jobs. PowerShell reference
From the PowerShell reference 16 parameters
What it does
Changes scheduled jobs. The Set-ScheduledJob cmdlet changes the properties of scheduled jobs, such as the commands that the jobs run or the credentials required to run the job. You can also use it to clear the execution history of the scheduled job. To use this cmdlet, begin by using the Get-ScheduledJob cmdlet to get the scheduled job. Then, pipe the scheduled job to Set-ScheduledJob or save the job in a variable and use the InputObject parameter to identify the job. Use the remaining parameters of Set-ScheduledJob to change the job properties or clear the execution history. Although you can use Set-ScheduledJob to change the triggers and options of a scheduled job, the Add-JobTrigger, Set-JobTrigger, and Set-ScheduledJobOption cmdlets provide much easier ways to accomplish those tasks. To create a new scheduled job, use the Register-ScheduledJob cmdlet. The Trigger parameter of Set-ScheduledJob adds one or more job triggers that start the job. The Trigger parameter is optional, so you can add triggers when you create the scheduled job, add job triggers later, add the RunNow parameter to start the job immediately, use the Start-Job cmdlet to start the job immediately at any time, or save the untriggered scheduled job as a template for other jobs. Set-ScheduledJob is one of a collection of job scheduling cmdlets in the PSScheduledJob module that is included in Windows PowerShell. For more information about Scheduled Jobs, see the About topics in the PSScheduledJob module. Import the PSScheduledJob module and then type: Get-Help about_Scheduled* or see about_Scheduled_Jobs. This cmdlet was introduced in Windows PowerShell 3.0.
Set-ScheduledJob [-Name <String>] [-ScriptBlock <ScriptBlock>] [-Trigger <ScheduledJobTrigger[]>] [-InitializationScript <ScriptBlock>] [-RunAs32] [-Credential <PSCredential>] [-Authentication <AuthenticationMechanism>] [-ScheduledJobOption <ScheduledJobOptions>] [-InputObject] <ScheduledJobDefinition> [-MaxResultCount <Int32>] [-PassThru] [-ArgumentList <Object[]>] [-RunNow] [-RunEvery <TimeSpan>] [<CommonParameters>]Set-ScheduledJob [-Name <String>] [-FilePath <String>] [-Trigger <ScheduledJobTrigger[]>] [-InitializationScript <ScriptBlock>] [-RunAs32] [-Credential <PSCredential>] [-Authentication <AuthenticationMechanism>] [-ScheduledJobOption <ScheduledJobOptions>] [-InputObject] <ScheduledJobDefinition> [-MaxResultCount <Int32>] [-PassThru] [-ArgumentList <Object[]>] [-RunNow] [-RunEvery <TimeSpan>] [<CommonParameters>]Set-ScheduledJob [-InputObject] <ScheduledJobDefinition> [-ClearExecutionHistory] [-PassThru] [<CommonParameters>]
Change the script that a job runs
Get-ScheduledJob -Name "Inventory"IdentifyDelete the execution history of a scheduled job
Get-ScheduledJob BackupArchive | Set-ScheduledJob -ClearExecutionHistoryIdentifyChange scheduled jobs on a remote computer
Invoke-Command -Computer "Server01, Server02" -ScriptBlock {Get-ScheduledJob |
Set-ScheduledJob -InitializationScript \\SrvA\Scripts\SetForRun.ps1}IdentifyIts page in the reference: Set-ScheduledJob.
Parameters
-ArgumentListSpecifies values for the parameters of the script that is specified by the FilePath parameter or for the command that is specified by the ScriptBlock parameter.-AuthenticationSpecifies the mechanism that is used to authenticate the user's credentials. The acceptable values for this parameter are:
- Default
- Basic
- Credssp
- Digest
- Kerberos
- Negotiate
- NegotiateWithImplicitCredential
The default value is Default. For more information about the values of this parameter, see AuthenticationMechanism Enumeration in the PowerShell SDK.
CAUTION: Credential Security Support Provider (CredSSP) authentication, in which the user's credentials are passed to a remote computer to be authenticated, is designed for commands that require authentication on more than one resource, such as accessing a remote network share. This mechanism increases the security risk of the remote operation. If the remote computer is compromised, the credentials that are passed to it can be used to control the network session.-ClearExecutionHistoryDeletes the current execution history and the saved results of the scheduled job.
The job execution history and job results are saved with the scheduled job in the
$HOME\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs directory on the computer on which
the job is created. To see the execution history, use the Get-Job cmdlet. To get the job results,
use the Receive-Job cmdlet.
This parameter does not affect the events that Task Scheduler writes to the Windows event logs and it does not stop Windows PowerShell from saving job results. To manage the number of job results that are saved, use the MaxResultCount parameter.-CredentialSpecifies a user account that has permission to run the scheduled job. The default is the current user.-FilePathSpecifies a script that the scheduled job runs. Enter the path to a .ps1 file on the local computer. To specify default values for the script parameters, use the ArgumentList parameter. Every scheduled job must have either a ScriptBlock or FilePath value.-InitializationScriptSpecifies the fully qualified path to a Windows PowerShell script (.ps1). The initialization script runs in the session that is created for the background job before the commands that are specified by the ScriptBlock parameter or the script that is specified by the FilePath parameter. You can use the initialization script to configure the session, such as adding files, functions, or aliases, creating directories, or checking for prerequisites.
To specify a script that runs the primary job commands, use the FilePath parameter.
If the initialization script generates an error, including a non-terminating error, the current instance of the scheduled job does not run and its status is Failed.-InputObjectSpecifies the scheduled job to be changed. Enter a variable that contains ScheduledJobDefinition
objects or type a command or expression that gets ScheduledJobDefinition objects, such as a
Get-ScheduledJob command. You can also pipe a ScheduledJobDefinition object to
Set-ScheduledJob.
If you specify multiple scheduled jobs, Set-ScheduledJob makes the same changes to all jobs.-MaxResultCountSpecifies how many job result entries are maintained for the scheduled job. The default value is 32.
Windows PowerShell saves the execution history and results of each triggered instance of the scheduled job on disk. The value of this parameter determines the number of job instance results that are saved for this scheduled job. When the number of job instance results exceeds this value, Windows PowerShell deletes the results of the oldest job instance to make room for the results of the newest job instance.
The job execution history and job results are saved in the
$HOME\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\<JobName>\Output\<Timestamp>
directories on the computer on which the job is created. To see the execution history, use the
Get-Job cmdlet. To get the job results, use the Receive-Job cmdlet.
The MaxResultCount parameter sets the value of the ExecutionHistoryLength property of the scheduled job.
To delete the current execution history and job results, use the ClearExecutionHistory parameter.-NameSpecifies a new name for the scheduled job and instances of the scheduled job. The name must be unique on the local computer.
To identify the scheduled job to be changed, use the InputObject parameter or pipe a scheduled job from Get-ScheduledJob to Set-ScheduledJob.
This parameter does not change the names of job instances on disk. It affects only job instances that are started after this command completes.-PassThruReturns an object representing the item with which you are working. By default, this cmdlet does not generate any output.-RunAs32Runs the scheduled job in a 32-bit process.-RunEveryUsed to specify how often to run the job. For example, use this option to run a job every 15 minutes.-RunNowStarts a job immediately, as soon as the Set-ScheduledJob cmdlet is run. This parameter eliminates the need to trigger Task Scheduler to run a Windows PowerShell script immediately after registration, and does not require users to create a trigger that specifies a starting date and time.-ScheduledJobOptionSets options for the scheduled job. Enter a ScheduledJobOptions object, such as one that you create by using the New-ScheduledJobOption cmdlet, or a hash table value.
You can set options for a scheduled job when you register the scheduled job or use the
Set-ScheduledJobOption or Set-ScheduledJob cmdlets to set or change options.
Many of the options and their default values determine whether and when a scheduled job runs. Be sure to review these options before scheduling a job. For a description of the scheduled job options, including the default values, see New-ScheduledJobOption.
To submit a hash table, use the following keys. In the following hash table, the keys are shown with their default values.
@{# Power SettingsStartIfOnBattery=$false;StopIfGoingOnBattery=$true; WakeToRun=$false; # Idle SettingsStartIfNotIdle=$false; IdleDuration="00:10:00"; IdleTimeout="01:00:00"; StopIfGoingOffIdle=$true; RestartOnIdleResume=$false;# Security settingsShowInTaskScheduler=$trueRunElevated=$false;# MiscRunWithoutNetwork=$false;DoNotAllowDemandStart=$false;MultipleInstancePolicy=IgnoreNew# Can be IgnoreNew, Parallel, Queue, StopExisting}-ScriptBlockSpecifies the commands that the scheduled job runs. Enclose the commands in braces ({}) to create a script block. To specify default values for command parameters, use the ArgumentList parameter.
Every Register-ScheduledJob command must use either the ScriptBlock or FilePath parameters.-TriggerSpecifies the triggers for the scheduled job. Enter one or more ScheduledJobTrigger objects, such as the objects that the New-JobTrigger cmdlet returns, or a hash table of job trigger keys and values.
A job trigger starts a scheduled job automatically on a one-time or recurring scheduled or when an event occurs.
Job triggers are optional. You can add a trigger when you create the scheduled job, use the
Add-JobTrigger or Set-ScheduledJob cmdlets to add triggers later, or use the Start-Job cmdlet
to start the scheduled job immediately. You can also create and maintain a scheduled job that has no
job triggers.
To submit a hash table, use the following keys.
@{Frequency="Once" (or Daily, Weekly, AtStartup, AtLogon);At="3am" (or any valid time string); DaysOfWeek="Monday", "Wednesday" (or any combination of day names); Interval=2 (or any valid frequency interval); RandomDelay="30minutes" (or any valid timespan string); User="Domain1\User01" (or any valid user; used only with the AtLogon frequency value)
}