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

Prior to SharePoint patching, it’s 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 expected.

So what are all the services to be stopped?

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 need 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 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") > > #Call the function to Stop or Start Services StartOrStop-SPFarm -StartOrStopOption "Stop" #StartOrStop-SPFarm -StartOrStopOption "Start"