PowerShell Restart Service: Restarting Across Multiple Devices

We recently had a requirement to restart a single service across a large number of devices on the network. The plan had originally been for the helpdesk to log on to each machine individually and restart it by hand — instead, this is easily scripted with PowerShell. PowerShell Script This simple script reads a text […]

Restart A Service On Multiple Devices Using Powershell
We recently had a requirement to restart a single service across a large number of devices on the network. The plan had originally been for the helpdesk to log on to each machine individually and restart it by hand — instead, this is easily scripted with PowerShell.

PowerShell Script

This simple script reads a text file containing a list of device names and restarts the target service on each one in turn. Run it from an elevated PowerShell session:
Get-Content C:\TGH\serverlist.txt | ForEach-Object { Restart-Service -InputObject $(Get-Service -Computer $_ -Name Spooler) }

Variables

The two things you’ll want to change:
  • C:\TGH\serverlist.txt — the path to a text file containing the NETBIOS name of each target device, one per line. You can build this list manually, or export it from Active Directory or Configuration Manager data.
  • Spooler — the service name (not the display name) of the service you want to restart. This example uses the Print Spooler service, whose service name is Spooler.
Run the script and it will work through the full list, restarting the named service on each device in sequence.

Running it in parallel

The script above processes devices one at a time, which is fine for a handful of machines but slow across a large list. If you’re targeting many devices and have PowerShell remoting enabled, Invoke-Command will run against all of them concurrently instead:
Invoke-Command -ComputerName (Get-Content C:\TGH\serverlist.txt) -ScriptBlock { Restart-Service -Name Spooler }

Feedback

If you have any questions or feedback on this guide, then please feel free to leave us a message below in our comments section. Don’t forget that you can also download this guide as a free PDF eBook.

Resources

Spooler Service

Discover more from TechyGeeksHome

Subscribe to get the latest posts sent to your email.

Andrew Armstrong

Andrew Armstrong is a UK-based IT professional with 26+ years of hands-on experience in Windows, Windows Server, SCCM/ConfigMgr, Active Directory, PowerShell, and enterprise infrastructure.

He founded TechyGeeksHome in 2010 and has published over 1,500 practical guides covering real-world IT problems and solutions. When not solving IT problems,

Andrew develops free Windows utilities including Ultimate Settings Panel, which has been downloaded over 850,000 times.