Menu & Search
PowerShell Restart Services on Multiple Hosts

PowerShell Restart Services on Multiple Hosts

This post contains a script that will help you restart services on multiple hosts at the same time using PowerShell.

To run a command in PowerShell on remote machines, we can use the Invoke-Command cmdlet. This guide is an amendment of Example Number 6: Run a single command on several computers on the cmdlet MS Docs link.

As per the post title, I’m running a script that will restart services on multiple hosts via PowerShell. The remote host list is to be populated with your hostnames:

# Restart service on multiple hosts
$parameters = @{
  ComputerName = 
  	"Hostname1",
  	"Hostname2",
  	"Hostname3"
  ScriptBlock = { Restart-Service *SQLSERVERAGENT* }
}
Invoke-Command @parameters
PowerShell Restart-Service

You can amend the ScriptBlock argument to a different PowerShell command of your choice, or perhaps add error checking if building something useful.

As always, hope to follow this up with more posts on the same topic. Until then, you can check out related content in the PowerShell Tips tag.

Share

0 Comments