How to Stop and Start All SharePoint 2013 Farm Services using PowerShell?

Prior to SharePoint patching, its a best practice to Stop all SharePoint 2013 and its related services and then start once patching is completed. If you don’t do this, your service pack or patch installation will take longer than its expected.

So what are all the services to be stopped?

  • SharePoint 2013 Search Service (OSearch15 – OSearch16 in SharePoint 2016)
  • SharePoint 2013 Timer Job (SPTimerV4)
  • SharePoint 2013 Administration (SPAdminV4)
  • SharePoint 2013 Tracing (SPTraceV4)
  • SharePoint 2013 VSS Writer (SPWriterV4)
  • SharePoint 2013 User Code Host (SPUserCodeV4)
  • SharePoint Search Host Controller (SPSearchHostController)
  • Forefront Sync Service (FIMSynchronizationService)
  • Forefront Service (FIMService)
  • World Wide Web Publishing Service (W3SVC)
  • Internet Information Services (IIS)
How to Stop and Start SharePoint 2013 Farm using PowerShell
Don’t forget to do it in all your SharePoint Servers of the farm!

Stop all SharePoint 2013 Services

Let’s use PowerShell to stop and start all SharePoint services:

Add-PSSnapin Microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$SharePointServices = ('SPSearchHostController','OSearch15','SPWriterV4','SPUserCodeV4','SPTraceV4','SPTimerV4','SPAdminV4','FIMSynchronizationService','FIMService','W3SVC')

#Stop all SharePoint Services
foreach ($Service in $SharePointServices)
{
    Write-Host -ForegroundColor red "Stopping $Service..."
    Stop-Service -Name $Service
}

#Stop IIS
iisreset /stop

Start all SharePoint 2013 Services:

After the patching, use the below script to start all SharePoint services.

Add-PSSnapin Microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$SharePointServices = ('SPSearchHostController','OSearch15','SPWriterV4','SPUserCodeV4','SPTraceV4','SPTimerV4','SPAdminV4','FIMSynchronizationService','FIMService','W3SVC')

#Start all SharePoint Services
foreach ($Service in $SharePointServices)
{
    Write-Host -ForegroundColor green "Starting $Service..."
    Start-Service -Name $Service
}

#Start IIS
iisreset /start

You may need this script when you want to do some maintenance activity on your SharePoint SQL databases and needed to stop all SharePoint connections! Please note, based on the server roles, You may have to stop distributed cache services: DCLoadBalancer15 , DCLauncher15.

Completely Stop or Start SharePoint Farm Services on All Servers:

Let’s put everything together and make a reusable PowerShell function, which stops or starts all SharePoint related services in all servers of the farm.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function StartOrStop-SPFarm()
{
    Param(
    [parameter(Mandatory=$true)] $StartOrStopOption
    )

    #Get All Servers in the Farm 
    $Farm = Get-SPFarm
    $Servers = $Farm.Servers | Where-Object {$_.Role -ne [Microsoft.SharePoint.Administration.SPServerRole]::Invalid} 
    Write-Host "Total Number of Servers in the Farm: " $Servers.Count 

    #List of All SharePoint Services
    $SharePointServices = ('SPSearchHostController','OSearch15','SPWriterV4','SPUserCodeV4','SPTraceV4','SPTimerV4','SPAdminV4','FIMSynchronizationService','FIMService','W3SVC','DCLoadBalancer15', 'DCLauncher15')

    #Iterate through each server
    $Servers | ForEach-Object {
     Write-Host "Performing Operation on Server:" $_.Name
       
        #Loop through each service
     foreach($ServiceName in $SharePointServices)
     {
      $ServiceInstance = Get-Service -ComputerName $_.Name -Name $ServiceName -ErrorAction SilentlyContinue
      if($ServiceInstance -ne $null)
      {
                If($StartOrStopOption -eq "Stop")
                {
                    Try 
                    {
                        Write-Host "Attempting to stop service" $ServiceName ".." -ForegroundColor Yellow
                        Stop-Service -InputObject $ServiceInstance
                        Write-Host "Stopped Service" $ServiceName -ForegroundColor Green 
                    }

                   catch 
                    {
                       Write-Host "Error Occured on Stopping Service. " $_.Message  -ForegroundColor Red 
                    }
                }
                elseif ($StartOrStopOption -eq "Start")
                {
                    Try 
                    {
                        Write-Host "Attempting to start service" $ServiceName ".." -ForegroundColor Yellow
                        Start-Service -InputObject $ServiceInstance
                        Write-Host "Started Service" $ServiceName -ForegroundColor Green 
                    }
                   catch 
                    {
                     Write-Host "Error Occured on Starting Service. " $_.Message  -ForegroundColor Red 
                    }   
                }
           }
     }
        #Start of Stop IIS
        If($StartOrStopOption -eq "Stop") { iisreset /stop} elseif ($StartOrStopOption -eq "Start") {iisreset /start}
    }
}

#Call the function to Stop or Start Services
StartOrStop-SPFarm -StartOrStopOption "Stop"
#StartOrStop-SPFarm -StartOrStopOption "Start"

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “How to Stop and Start All SharePoint 2013 Farm Services using PowerShell?

  • November 1, 2018 at 10:09 AM

    Thanks for providing this code. Easier to run this than clicking through all the services!

    Also, I found for a standalone SharePoint 2016 server (my development server) the services I needed to stop were:

    $SharePointServices = (‘SPSearchHostController’,’SPTimerV4′,’SPTraceV4′,’SPUserCodeV4′,’SPWriterV4′,’W3SVC’,’OSearch16′)

    Cheers,

    Tom Cuffe

    Reply
  • October 31, 2018 at 2:29 PM

    great article as usual

    Reply

Leave a Reply