- Add a domain user or group to local administrators with PowerShell - Wed, Mar 19 2014
- Create a list of local administrators with PowerShell - Wed, Mar 5 2014
- Remotely query user profile information with PowerShell - Tue, Nov 26 2013
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
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
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!