Using PowerShell to Restart a Service on Multiple Remote Machines

Hi All,

There may be times when you will need to restart a service on multiple servers whether it be some sort of configuration change or update. To save yourself from having to either logon to each server or connect one by one using remote tools, you can simply use PowerShell to restart the service. First I will start by demonstrating how to restart a service on one remote machine and then I will show how to restart a service on multiple remote computers. You will need to open up Windows Remote Management WinRM on the remote computers before you run PowerShell commands against them remotely (Windows 2012 has this enabled by default). Configuring WinRM is outside the scope of this article. This article assumes that WinRM is already configured. This article also assumes the remote service being restarted does NOT have any dependent services.

Before I begin I would like to show you how to get the name of a service and services information in general using PowerShell. All you need to do is type the following command to list all the services on a machine as well as the Status, Name and DisplayName. In the example below, I will get the services for a server named PRINTSERVER. Open PowerShell and enter the following command:

Get-Service -ComputerName PRINTSERVER

As mentioned earlier, this article assumes that the remote service you are restarting does not have other services dependent on it. To see if a service has dependent services on the remote machine you can use the –DependentServices parameter. Type the following to find out if there are dependent services for the spooler service on a server named PRINTSERVER:

Get-Service -ComputerName PRINTSERVER -name spooler –DependentServices

If the above command returns no results you will know that there are no dependent services.

Now that you have a list of the services, I will demonstrate how to restart the print spooler service on a single machine named “PRINTSERVER” in the example below. Replace PRINTSERVER with the name of the server or workstation for which you would like to restart the service. Also replace “spooler” with the name of the service you would like to restart. Make sure you are running PowerShell with an account that has access to restart services on the remote computer. Since the “Restart-Service” cmdlet does not have a parameter to manage services on remote machines, we use the “Invoke-Command” cmdlet. Run the following command:

Invoke-Command -Computername PRINTSERVER {Restart-Service spooler}

Next I want to show you how to restart services on multiple remote machines. First we will need to create a text file with the names of the machines for which we would like to restart the specified services. Open Notepad and type a list of servers line by line that you want the “Restart-Service” command to be executed against as shown below. Save the text file. In this example I am saving the file as Servers.txt.

Now we will execute the 2 lines below to restart the spooler service on servers PRINTSERV1 and PRINTSERV2.

$computers = Get-Content “c:\Scripts\servers.txt”
Invoke-Command -ComputerName $computers {Restart-Service Spooler}

If you check the system log on the remote machines you will see that the service you specified was restarted.

I hope this helps. It’s always a good idea to test this and other commands in a lab scenario first. If you have any questions or feedback, please leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *