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)
Lets use PowerShell to stop and start all SharePoint services:
Stop all SharePoint 2013 Services, Lets use PowerShell to stop and start all SharePoint services:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 1 Add-PSSnapin Microsoft.sharepoint.powershell -ErrorAction SilentlyContinue 2 3 $SharePointServices = ('SPSearchHostController','OSearch15','SPWriterV4','SPUserCodeV4','SPTraceV4','SPTimerV4','SPAdminV4','FIMSynchronizationService','FIMService','W3SVC') 4 5 #Stop all SharePoint Services 6 foreach ($Service in $SharePointServices) 7 { 8 Write-Host -ForegroundColor red "Stopping $Service..." 9 Stop-Service -Name $Service 10 } 11 12 #Stop IIS 13 iisreset /stop |
Start all SharePoint 2013 Services: After the patching, Use the below script to start all SharePoint services.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 1 Add-PSSnapin Microsoft.sharepoint.powershell -ErrorAction SilentlyContinue 2 3 $SharePointServices = ('SPSearchHostController','OSearch15','SPWriterV4','SPUserCodeV4','SPTraceV4','SPTimerV4','SPAdminV4','FIMSynchronizationService','FIMService','W3SVC') 4 5 #Start all SharePoint Services 6 foreach ($Service in $SharePointServices) 7 { 8 Write-Host -ForegroundColor green "Starting $Service..." 9 Start-Service -Name $Service 10 } 11 12 #Start IIS 13 iisreset /start |
Completely Stop or Start SharePoint Farm Services on All Servers: Lets put everything together and make a reusable PowerShell function, which stops or starts all SharePoint related services in all servers of the farm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 2 3 Function StartOrStop-SPFarm() 4 { 5 Param( 6 [parameter(Mandatory=$true)] $StartOrStopOption 7 ) 8 9 #Get All Servers in the Farm 10 $Farm = Get-SPFarm 11 $Servers = $Farm.Servers | Where-Object {$_.Role -ne [Microsoft.SharePoint.Administration.SPServerRole]::Invalid} 12 Write-Host "Total Number of Servers in the Farm: " $Servers.Count 13 14 #List of All SharePoint Services 15 $SharePointServices = ('SPSearchHostController','OSearch15','SPWriterV4','SPUserCodeV4','SPTraceV4','SPTimerV4','SPAdminV4','FIMSynchronizationService','FIMService','W3SVC','DCLoadBalancer15', 'DCLauncher15') 16 17 #Iterate through each server 18 $Servers | ForEach-Object { 19 Write-Host "Performing Operation on Server:" $_.Name 20 21 #Loop through each service 22 foreach($ServiceName in $SharePointServices) 23 { 24 $ServiceInstance = Get-Service -ComputerName $_.Name -Name $ServiceName -ErrorAction SilentlyContinue 25 if($ServiceInstance -ne $null) 26 { 27 If($StartOrStopOption -eq "Stop") 28 { 29 Try 30 { 31 Write-Host "Attempting to stop service" $ServiceName ".." -ForegroundColor Yellow 32 Stop-Service -InputObject $ServiceInstance 33 Write-Host "Stopped Service" $ServiceName -ForegroundColor Green 34 } 35 36 catch 37 { 38 Write-Host "Error Occured on Stopping Service. " $_.Message -ForegroundColor Red 39 } 40 } 41 elseif ($StartOrStopOption -eq "Start") 42 { 43 Try 44 { 45 Write-Host "Attempting to start service" $ServiceName ".." -ForegroundColor Yellow 46 Start-Service -InputObject $ServiceInstance 47 Write-Host "Started Service" $ServiceName -ForegroundColor Green 48 } 49 50 catch 51 { 52 Write-Host "Error Occured on Starting Service. " $_.Message -ForegroundColor Red 53 } 54 } 55 } 56 } 57 #Start of Stop IIS 58 If($StartOrStopOption -eq "Stop") { iisreset /stop} elseif ($StartOrStopOption -eq "Start") {iisreset /start} 59 } 60 } 61 62 #Call the function to Stop or Start Services 63 StartOrStop-SPFarm -StartOrStopOption "Stop" 64 #StartOrStop-SPFarm -StartOrStopOption "Start" |