Export (0) Print
Expand All

Set-ScheduledJob

Updated: May 7, 2014

Applies To: Windows PowerShell 4.0

Set-ScheduledJob

Changes scheduled jobs

Syntax

Parameter Set: ScriptBlock
Set-ScheduledJob [-InputObject] <ScheduledJobDefinition> [-ArgumentList <Object[]> ] [-Authentication <AuthenticationMechanism> ] [-Credential <PSCredential> ] [-InitializationScript <ScriptBlock> ] [-MaxResultCount <Int32> ] [-Name <String> ] [-PassThru] [-RunAs32] [-RunNow] [-ScheduledJobOption <ScheduledJobOptions> ] [-ScriptBlock <ScriptBlock> ] [-Trigger <ScheduledJobTrigger[]> ] [ <CommonParameters>]

Parameter Set: Execution
Set-ScheduledJob [-InputObject] <ScheduledJobDefinition> [-ClearExecutionHistory] [-PassThru] [ <CommonParameters>]

Parameter Set: FilePath
Set-ScheduledJob [-InputObject] <ScheduledJobDefinition> [-ArgumentList <Object[]> ] [-Authentication <AuthenticationMechanism> ] [-Credential <PSCredential> ] [-FilePath <String> ] [-InitializationScript <ScriptBlock> ] [-MaxResultCount <Int32> ] [-Name <String> ] [-PassThru] [-RunAs32] [-RunNow] [-ScheduledJobOption <ScheduledJobOptions> ] [-Trigger <ScheduledJobTrigger[]> ] [ <CommonParameters>]




Detailed Description

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 of Set-ScheduledJob 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 is introduced in Windows PowerShell 3.0.

Parameters

-ArgumentList<Object[]>

Specifies 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Authentication<AuthenticationMechanism>

Specifies the mechanism that is used to authenticate the user's credentials. Valid values are Default, Basic, Credssp, Digest, Kerberos, Negotiate, and NegotiateWithImplicitCredential. The default value is Default. For information about the values of this parameter, see the description of the System.Management.Automation.Runspaces.AuthenticationMechanism enumeration in MSDN.

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.


Aliases

none

Required?

false

Position?

named

Default Value

Default

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-ClearExecutionHistory

Deletes 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.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Credential<PSCredential>

Specifies a user account that has permission to run the scheduled job. The default is the current user.

Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one from the Get-Credential cmdlet. If you enter only a user name, you will be prompted for a password.


Aliases

none

Required?

false

Position?

named

Default Value

Current user

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-FilePath<String>

Specifies 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-InitializationScript<ScriptBlock>

Specifies 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 (even a non-terminating error), the current instance of the scheduled job does not run and its status is "Failed."


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-InputObject<ScheduledJobDefinition>

Specifies 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.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

True (ByValue)

Accept Wildcard Characters?

false

-MaxResultCount<Int32>

Specifies 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.


Aliases

none

Required?

false

Position?

named

Default Value

32

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Name<String>

Specifies 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-PassThru

Returns the scheduled jobs that changed. By default, this cmdlet does not generate any output.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-RunAs32

Runs the scheduled job in a 32-bit process.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-RunNow

Starts 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-ScheduledJobOption<ScheduledJobOptions>

Sets 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}


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-ScriptBlock<ScriptBlock>

Specifies 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Trigger<ScheduledJobTrigger[]>

Specifies 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) }


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition

    You can pipe scheduled jobs to Set-ScheduledJob.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • None or Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition

    If you use the Passthru parameter, Set-ScheduledJob returns the scheduled job that was changed. Otherwise, this cmdlet does not generate any output.


Examples

Example 1: Change the script that a job runs

This example shows how to change the script that is run in a scheduled job.


 

The first command uses the Get-ScheduledJob cmdlet to get the Inventory scheduled job. The output shows that the job runs the Get-Inventory.ps1 script.

This command is not required; it is included only to show the effect of the script change.


PS C:\> Get-ScheduledJob -Name Inventory
Id         Name            Triggers        Command                                  Enabled
-- ---- -------- ------- -------
1 Inventory {1} C:\Scripts\Get-Inventory.ps1 True

 

The second command uses the Get-ScheduledJob cmdlet to get the Inventory scheduled job. A pipeline operator (|) sends the scheduled job to the Set-ScheduledJob cmdlet. The Set-ScheduledJob command uses the Script parameter to specify a new script, Get-FullInventory.ps1. The command uses the Passthru parameter to return the scheduled job after the change.


PS C:\> Get-ScheduledJob -Name Inventory | Set-ScheduledJob -FilePath C:\Scripts\Get-FullInventory.ps1 -Passthru
Id         Name            Triggers        Command                                  Enabled
-- ---- -------- ------- -------
1 Inventory {1} C:\Scripts\Get-FullInventory.ps1 True

Example 2: Delete the execution history of a scheduled job

This command deletes the current execution history and saved job results for the BackupArchive scheduled job.

The command uses the Get-ScheduledJob cmdlet to get the BackupArchive scheduled job. A pipeline operator (|) sends the job to the Set-ScheduledJob cmdlet to change it. The Set-ScheduledJob command uses the ClearExecutionHistory parameter to delete the execution history and saved results.

For more information about the execution history and saved job results of scheduled jobs, see about_Scheduled_Jobs.


PS C:\> Get-ScheduledJob BackupArchive | Set-ScheduledJob -ClearExecutionHistory

Example 3: Change scheduled jobs on a remote computer

This command changes the initialization script in all scheduled jobs on the Server01 and Server02 computers.

The command uses the Invoke-Command cmdlet to run a command on the Server01 and Server02 computers.

The remote command begins with a Get-ScheduledJob command that gets all scheduled jobs on the computer. The scheduled jobs are piped to the Set-ScheduledJob cmdlet which changes the initialization script to SetForRun.ps1.


PS C:\> Invoke-Command -Computer Server01, Server02 -ScriptBlock {Get-ScheduledJob | Set-ScheduledJob -InitializationScript \\SrvA\Scripts\SetForRun.ps1}

Related topics



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft