Introduction

This article is a sequence of real world examples using PowerShell in SharePoint 2016. In this article, we will discuss how we will use the PowerShell cmdlets for managing the timer jobs in SharePoint. We will try to cover all the available cmdlets i.e get, set, enable, disable and start.

Scenario

Customer is having some serious issues with deleted sites and quota requests. They want to stop the job from a further run before they take care the situation. Here are the details.

They want to immediately disable the timer jobs, then change the schedule of both jobs. Meanwhile, they will do their fixes. Now they want to enable the timer job and immediately run both jobs so that the backlog is cleared and processed. 

Steps

Split both timer jobs into two examples, but steps for both will be the same with slightly different options.
  • Get the id of the timer job
  • Disable the timer and check the status
  • Change the schedule of the timer job
  • Enable the timer job
  • Run the job immediately

Example # 1

In the first example, we will use the "Request more quota" timer jobs and perform all the steps.

Get ID

MR admin runs the Get-SPTimerJob command but it returns all the timer jobs and it's very hard to identify the required timer jobs.
Get-SPTimerJob

Output will be like this:

1

Now try with different options and run the below command. We apply a Where filter on the DisplayName property of TimerJobs and save the output in a $job variable. In the second line, we use the $job variable and pipe it with Select to show only required properties and format it as a table to show everything.

$job = Get-SPTimerJob | Where-Object {$_.displayname –like “Request more quota”}  
$job | Select name, DisplayName, ID, schedule | Format-Table

This will return the timer job information related to Request More Quota. Important thing: if you have multiples web applications in your farm then use the -webapplication switch to get the timer job for the specified web app.

2

Disable

Now we have the Job ID and we disable it using the below command. We use the Disable-SptimerJob command to disable it.
Get-SPTimerJob $job | Disable-SPTimerJob

$job | Select  DisplayName, IsDisabled

 

This will disable Request More Quota and the output shows IsDisabled as true or false, like this:

5

Or we can do the same thing with the below PowerShell.

Disable-SPTimerJob “job-quota-requestmore”

$job | Select  DisplayName, IsDisabled

Set The Schedule

After admins do their fix on the backend, we want to change the schedule of this timer job. For this we will use the Set-SpTimerJob command. We have to pass the -Schedule Parameter in the supported format.
Get-SPTimerJob $job | Set-SPTimerJob -Schedule "Daily at 9:00"

$job | Select name, DisplayName, ID, schedule | Format-Table

This code will set the schedule of the of the timer job daily at 9:00:00 and show us the output like this:

4

Or we can do the same a slightly different way but the output will be same.

Set-SPTimerJob “job-quota-requestmore” -Schedule "Daily at 9:00"

$job | Select name, DisplayName, ID, schedule, lastruntime | Format-Table

Enabling

Now we want to enable the timer job.
Get-SPTimerJob $job | Enable-SPTimerJob 

$job | Select  DisplayName, IsDisabled

This code will enable the disabled timer job (job-quota-requestmore).  The output will be likely this:

3

Or we can use the below code to enable the same timer job. We simply give the name of the timer job.

Enable-SPTimerJob “job-quota-requestmore”

$job | Select  DisplayName, IsDisabled

Start Timer Job

The last step of the example, we have to start the job.
Get-SPTimerJob $job | Start-SPTimerJob 

$job | Select name, DisplayName, ID, schedule, lastruntime | Format-Table

This code will start the job immediately, and the second line shows us the last run time. Like this:

6

Or we can do the same thing in a different way.

Start-SPTimerJob  “job-quota-requestmore”

$job | Select name, DisplayName, ID, schedule, lastruntime | Format-Table

Example # 2

In the second example, we use the "Gradual Site Deletion" timer jobs and perform all the steps.

Get ID

MR admin will run the below command. In this, we apply a Where filter on the DisplayName property of TimerJobs and save the output in a $job variable. In the second line, we use the $job variable and pipe it with a Select to show only required properties and format it as a table to show everything.
$job = Get-SPTimerJob | Where-Object {$_.displayname –like “Gradual Site Deletion”}  
$job | Select name, DisplayName, ID, schedule | Format-Table

This will return the timer job information related to Gradual Site Deletion. Important Thing: if you have multiple web applications in your farm then use the -webapplication switch to get the timer job for the specified web app.

2-1

Disable

Now we have the Job ID and we will disable it using the below command. We will use the Disable-SPTimerJob command to disable it.
Get-SPTimerJob $job | Disable-SPTimerJob

$job | Select  DisplayName, IsDisabled

 

This will disable the Request More Quota and the output shows IsDisabled as true or false like this:

2-2

Or we can do the same thing with PowerShell.

Disable-SPTimerJob “job-site-deletion”

$job | Select  DisplayName, IsDisabled

Set the Schedule

After admins do their fix on the backend, we want to change the schedule of this timer job. For this we will use the Set-SpTimerJob command, We have to pass the -Schedule Parameter in the supported format.
Get-SPTimerJob $job | Set-SPTimerJob -Schedule "Daily at 9:00"

$job | Select name, DisplayName, ID, schedule | Format-Table

This code will set the schedule of the of the timer job daily at 9:00:00 and show us the output like this:

2-3

Or we can do same in a slightly different way but the output will be same.

Set-SPTimerJob “job-site-deletion” -Schedule "Daily at 9:00"

$job | Select name, DisplayName, ID, schedule, lastruntime | Format-Table

Enabling

Now we want to enable the timer job.
Get-SPTimerJob $job | Enable-SPTimerJob 

$job | Select  DisplayName, IsDisabled

This code will enable the disabled timer job (job-site-deletion).  Output will be likely this:

2-4

Or we can use the below code to enable the same timer job. We simply give the name of the timer job.

Enable-SPTimerJob “job-site-deletion”

$job | Select  DisplayName, IsDisabled

Start Timer Job

The last step of the example is to start the job.
Get-SPTimerJob $job | Start-SPTimerJob 

$job | Select name, DisplayName, ID, schedule, lastruntime | Format-Table

This code will start the job immediately, and the second line will show us the last run time. Like this:

2-5

Or we can do the same thing in a different way.

Start-SPTimerJob  “job-site-deletion

$job | Select name, DisplayName, ID, schedule, lastruntime | Format-Table

Note:

This is an example and the situation will vary in your environment. This example is just for learning purposes and gives you an idea how to use the above PowerShell commands.

See Also: