Manage SharePoint 2010 Services with PowerShell

As you may already know, you can use the “Manage Services on Server” option from Central Administration to centrally start and stop service instances across all SharePoint servers in your farm. An example of this administration page is shown below:

The issue with this page is that it can be cumbersome to use if there are a few servers in your farm and you keep having to click the “Server” drop-down to select one of them. It is also not very repeatable and there may be issues where you might want to automate the process – for example, stopping the User Profile Synchronization Service before a backup or application update and restarting it once complete.

You can get a similar list to the one shown in Central Admin using PowerShell by typing the following command:

Get-SPServiceInstance -Server PAC-SP2010 | sort TypeName | Format-Table -AutoSize

 

This will not only provide the service name and status, but also the service instance GUID:

There are a couple of ways to stop a service instance. Firstly, the most user friendly method, which is to use the service display name and then call the Stop-SPServiceInstance cmdlet:

Get-SPServiceInstance -Server PAC-SP2010" | where-object {$_.TypeName -eq "Managed Metadata Web Service" } | Stop-SPServiceInstance -Confirm:$false

 

There are a couple of things to note here:

  1. The -Server switch is not required if you are configuring service instances for the server you are currently logged into
  2. The -Confirm:$false switch is also optional. Setting this to $false supresses the confirmation prompt that appears when attempting to stop the service instance through PowerShell

You can also use the Stop-SPServiceInstance cmdlet directly by specifying the service instance GUID, as shown below for the Managed Metadata Web Service on this farm:

Stop-SPServiceInstance -Identity 3f36d0f4-ce39-48aa-a776-6300f1e2b58f -Confirm:$false

 

Restarting the service is simply a case of typing either of the same commands as before, but this time replacing Stop-SPServiceInstance with Start-SPServiceInstance.

First example:

Get-SPServiceInstance -Server PAC-SP2010" | where-object {$_.TypeName -eq "Managed Metadata Web Service" } | Start-SPServiceInstance -Confirm:$false

 

Second example:

Start-SPServiceInstance -Identity 3f36d0f4-ce39-48aa-a776-6300f1e2b58f -Confirm:$false

 

That’s great, but you also might want to check a service instance has a particular status before attempting to stop or start it. In the example below, the Managed Metadata Web Service instance is associated with the $serviceinstance variable and the Status is checked to make sure it is “Online” before attempting to stop the service instance:

$serviceinstance = Get-SPServiceInstance -Server PAC-SP2010" | where-object {$_.TypeName -eq "Managed Metadata Web Service" }
if ($serviceinstance.Status -eq "Online")
{
Write-Host "Stopping Managed Metadata Web Service on PAC-SP2010"
try
{
$serviceinstance | Stop-SPServiceInstance -Confirm:$false
}
catch { Write-Host "There was an error:" $_ }
}
else
{
Write-Host "Managed Metadata Web Service on PAC-SP2010 has already been stopped"
}