SharePoint: How To re-start SharePoint Timer Service using PowerShell / Custom Code!

In this article I will show you how to re-start al the instances of the SharePoint Timer Service in a SharePoint farm by means of PowerShell and the Server Side Object Model. Both approaches are quite similar as you will see in the code. Let’s start :-).

<span id="lnum1" style="color: #606060"> 1:</span> ####################################################################################

2: # This script allows to re- start all the SharePoint Timer Service instances in a SharePoint Farm

<span id="lnum3" style="color: #606060"> 3:</span> # Required <span style="color: #0000ff">Parameters</span>: N/A<!--CRLF-->
<span id="lnum4" style="color: #606060"> 4:</span> ####################################################################################<!--CRLF-->
<span id="lnum5" style="color: #606060"> 5:</span> <!--CRLF-->
<span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">If</span> ((<span style="color: #0000ff">Get</span>-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $<span style="color: #0000ff">null</span> ) <!--CRLF-->
<span id="lnum7" style="color: #606060"> 7:</span> { <span style="color: #0000ff">Add</span>-PSSnapIn -Name Microsoft.SharePoint.PowerShell }<!--CRLF-->
<span id="lnum8" style="color: #606060"> 8:</span> <!--CRLF-->
<span id="lnum9" style="color: #606060"> 9:</span> $<span style="color: #0000ff">host</span>.Runspace.ThreadOptions = "ReuseThread"<!--CRLF-->
<span id="lnum10" style="color: #606060"> 10:</span> <!--CRLF-->
<span id="lnum11" style="color: #606060"> 11:</span> #<!--CRLF-->
<span id="lnum12" style="color: #606060"> 12:</span> #Definition <span style="color: #0000ff">of</span> the <span style="color: #0000ff">function</span> that allows <span style="color: #0000ff">to</span> re-<span style="color: #0000ff">start</span> <span style="color: #0000ff">all</span> the SharePoint Timer Service instances <span style="color: #0000ff">in</span> a SharePoint Farm<!--CRLF-->
<span id="lnum13" style="color: #606060"> 13:</span> <span style="color: #0000ff">function</span> Re-StartSPTimerService<!--CRLF-->
<span id="lnum14" style="color: #606060"> 14:</span> {<!--CRLF-->
<span id="lnum15" style="color: #606060"> 15:</span> try<!--CRLF-->
<span id="lnum16" style="color: #606060"> 16:</span> {<!--CRLF-->
<span id="lnum17" style="color: #606060"> 17:</span> $spFarm=<span style="color: #0000ff">Get</span>-SPFarm<!--CRLF-->
<span id="lnum18" style="color: #606060"> 18:</span> $spfTimerServcicesInstance=$spFarm.TimerService.Instances<!--CRLF-->
<span id="lnum19" style="color: #606060"> 19:</span> foreach ($spfTimerServiceInstance <span style="color: #0000ff">in</span> $spfTimerServcicesInstances)<!--CRLF-->
<span id="lnum20" style="color: #606060"> 20:</span> {<!--CRLF-->
<span id="lnum21" style="color: #606060"> 21:</span> <span style="color: #0000ff">Write</span>-<span style="color: #0000ff">Host</span> "Re-starting the instance " $spfTimerServiceInstance.TypeName<!--CRLF-->
<span id="lnum22" style="color: #606060"> 22:</span> $spfTimerServiceInstance.Stop()<!--CRLF-->
<span id="lnum23" style="color: #606060"> 23:</span> $spfTimerServiceInstance.<span style="color: #0000ff">Start</span>()<!--CRLF-->
<span id="lnum24" style="color: #606060"> 24:</span> <span style="color: #0000ff">Write</span>-<span style="color: #0000ff">Host</span> "SharePoint Timer Service Instance" $spfTimerServiceInstance.TypeName "Re-Started"<!--CRLF-->
<span id="lnum25" style="color: #606060"> 25:</span> }<!--CRLF-->
<span id="lnum26" style="color: #606060"> 26:</span> }<!--CRLF-->
<span id="lnum27" style="color: #606060"> 27:</span> catch [System.<span style="color: #0000ff">Exception</span>]<!--CRLF-->
<span id="lnum28" style="color: #606060"> 28:</span> {<!--CRLF-->
<span id="lnum29" style="color: #606060"> 29:</span> <span style="color: #0000ff">write</span>-<span style="color: #0000ff">host</span> -f red $_.<span style="color: #0000ff">Exception</span>.ToString()<!--CRLF-->
<span id="lnum30" style="color: #606060"> 30:</span> }<!--CRLF-->
<span id="lnum31" style="color: #606060"> 31:</span> }<!--CRLF-->
<span id="lnum33" style="color: #606060"> 32:</span> <span style="color: #0000ff">Start</span>-SPAssignment –<span style="color: #0000ff">Global</span><!--CRLF-->
<span id="lnum34" style="color: #606060"> 33:</span> #Calling the <span style="color: #0000ff">function</span><!--CRLF-->
<span id="lnum35" style="color: #606060"> 34:</span> Re-StartSPTimerService<!--CRLF-->
<span id="lnum36" style="color: #606060"> 35:</span> Stop-SPAssignment –<span style="color: #0000ff">Global</span><!--CRLF-->
<span id="lnum38" style="color: #606060"> 36:</span> Remove-PSSnapin Microsoft.SharePoint.PowerShell

As you can see, once you get access to the SharePoint farm using the Get-SPFarm cmdlet, it’s really simple to get all the instances for the SharePoint Timer Service using the property TimerService.Instances. For each instance, you only have to call the Stop() and Start() methods in order to stop and start all the service instances.

  • In a similar way, you can use the SharePoint Server Side Object Model to stop and start the Timer Service instances using the following code:
<span id="lnum1" style="color: #606060"> 1:</span> SPFarm spfFarm = SPFarm.Local;<!--CRLF-->
<span id="lnum2" style="color: #606060"> 2:</span> SPServiceInstanceDependencyCollection sptsiCollection = <!--CRLF-->
<span id="lnum3" style="color: #606060"> 3:</span> spfFarm.TimerService.Instances; <!--CRLF-->
<span id="lnum4" style="color: #606060"> 4</span><span id="lnum5" style="color: #606060">:</span> <span style="color: #0000ff">foreach</span> (SPTimerServiceInstance spiTimerService <span style="color: #0000ff">in</span> sptsiCollection)<!--CRLF-->
<span id="lnum6" style="color: #606060"> 5:</span> {<!--CRLF-->
<span id="lnum7" style="color: #606060"> 6:</span> Console.WriteLine(<span style="color: #006080">"Re-Iniciando la instancia {0}"</span>, spiTimerService.TypeName);<!--CRLF-->
<span id="lnum8" style="color: #606060"> 7:</span> spiTimerService.Stop();<!--CRLF-->
<span id="lnum9" style="color: #606060"> 8:</span> spiTimerService.Start();<!--CRLF-->
<span id="lnum10" style="color: #606060"> 9:</span> Console.WriteLine(<span style="color: #006080">"Instancia del servicio {0} re-iniciada "</span>, spiTimerService.TypeName);<!--CRLF-->
<span id="lnum11" style="color: #606060"> 10:</span> }

And that’s all about how to manage the SharePoint Timer Service instances using PowerShell or the Server Side Object Model. Happy CloudSharing!!