POLL: POWERSHELL VS. GUI - DO YOU WANT TO BE A DEVOP OR AN ADMIN?

Restart a service on multiple remote computers using PowerShell

In this article, I will show you how to restart services on remote computers using PowerShell. I will also provide a sample PowerShell script that you can adjust for your purpose.

A picture of Sitaram Pamarthi By Sitaram Pamarthi - Fri, August 3, 2012 - 2 comments

Sitaram Pamarthi is working as a Windows Engineer and his special fields of interest are PowerShell, Active Directory, Exchange, and virtualization.

Restarting a service is quite a common system administration task. If a service has to be restarted on a local computer, we use Service Manager MMC (services.msc). We use the same MMC when a service restart is required on a remote computer. If you want to restart the same service on multiple computers, however, there is no built-in tool. The PowerShell code that I am going to talk about will help you here.

PowerShell is an easy way to restart a service on a remote computer. Though there are multiple ways to restart a service using PowerShell, I prefer the Restart-Service cmdlet. The script I am going to talk about is a wrapper around this cmdlet to get the functionality we need.

This script takes three arguments:

  • ComputerName: Here you can provide the list of computers on which to restart the service. This is an optional parameter. If you don’t use it, the script runs against the local computer.
  • ServiceName: Name of the service that you want to restart on remote computers. This is a mandatory property.
  • OutputDir: Name of the directory where you want to store the output files failedcomputers.txt and successcomputers.txt, which contain the status of the service restart.

You probably know that the Get-Service cmdlet can be used to query remote services. Only a few know that the Start-Service, Stop-Service, and Restart-Service cmdlets can be used to perform respective operations on remote computers even though they don’t have a –ComputerName parameter. The procedure is very simple. Instead of the –ComputerName parameter, we need to use the –InputObject parameter for these cmdlets to stop/start/restart services. The value for –InputObject can be derived using Get-Service. See the code below for more clarity.

$ServiceObj = Get-Service -Name $ServiceName -ComputerName $Computer
-ErrorAction Stop
Restart-Service -InputObj $ServiceObj -erroraction stop

The Get-Service cmdlet with –ComputerName will return an Object reference to the service we are querying. We can input this object to Start-Service, Stop-Service, or Restart-Service to perform the respective actions. The additional –ComputerName parameter is not needed, as I show in the above code. This is the core part of the script. Everything else is a wrapper on top of it.

I also used the Test-Connection cmdlet in the script to see if a computer is reachable before querying for services. I build a custom PsObject to store the output results, which will be displayed on the screen and filtered at the end of the script to save the details in failedComputers.txt and successComputers.txt. By default, these output files are created under c:\. If you want to change the location, use the –OutputDir parameter while running the script.

Samples to restart a service with the PowerShell script

Restart a service on a local computer:

.\Restart-Service –ServiceName dnscache

Restart a service on a remote computer:

.\Restart-Service -ComputerName Comp1 –ServiceName dnscache

Restart a service on Comp1 and Comp2:

.\Restart-Service –ComputerName Comp1, Comp2 -ServiceName dnscache

Take servers list from a text file and restart a service on them:

Get-Content c:\scripts\servers.txt | .\Restart-Service –ServiceName dnscache

This is the output of the command above:

PowerShell - Restart a remote service

PowerShell – Restart a remote service

For safety, try the script in a lab environment before you run it in production. Hope this script helps. The script is provided as is without warranty of any kind!

Your question wasn't answered? Ask in the new 4sysops forum!

2 Comments - Leave a Reply

  1. Mark Bradley says:

    Hello Sitaram, I am very interested in this subject as I have always used command line and the “SC” command to accomplish similar.
    You mention the creation ow wrappers and output files and I do not see that in your script. I follow the idea of what you are doing but I am missing the syntax. Can you provide a full script exampel rather than the bits and pieces.

    Thanks
    Mark

  2. techibee.com says:

    Hi Mark, thanks for your comment. You can download complete script using the download link given at the bottom of the article. That has the code for saving the results into text files.

    For example, keep all your servers in a single text file named servers.txt and pass it to script like below.

    .\restart-service -ComputerName (Get-Content c:\servers.txt) -ServiceName dnscache

===Leave a Comment===