Share via


powershell restart-service on many computers

Question

Wednesday, March 6, 2013 8:24 PM

I need to remote restart a service on a bunch of machines.  This command works great on a single machine:  

Restart-Service -InputObject $(Get-Service -Computer computer1 -Name service1);

But I need to pipe/input a list of many computers in place of computer1. Is there a quick and easy way to do that?

Thank you,

All replies (11)

Wednesday, March 6, 2013 8:35 PM âś…Answered | 6 votes

Get-Content C:\SomeFile.txt | ForEach-Object { Restart-Service -InputObject $(Get-Service -Computer $_ -Name service1) }

You can try that, if that doesn't work then I'd do

$computers = Get-Content C:\SomeFile.txt

ForEach ($computer in $computers)

{

  Restart-Service -InputObject $(Get-Service -Computer $computer -Name service1);

}

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


Wednesday, March 6, 2013 8:31 PM

How are you storing the computer names?

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


Wednesday, March 6, 2013 8:32 PM

just a text file


Wednesday, March 6, 2013 9:24 PM | 1 vote

 I use this to start services on remote systems:

$computers = get-content C:\temp\justatextfile.txt
ForEach ($entry in $computers)
{
Get-Service remoteregistry -ComputerName $entry | start-service 
}

Wednesday, March 6, 2013 9:26 PM | 2 votes

$computers = get-content C:\temp\justatextfile.txt
ForEach ($entry in $computers)
{
Get-Service remoteregistry -ComputerName $entry | stop-service 
}

Use this to stop a remote service.


Wednesday, March 6, 2013 9:54 PM

Get-Content C:\SomeFile.txt | ForEach-Object { Restart-Service -InputObject $(Get-Service -Computer $_ -Name service1) }

That's perfect, thanks,


Sunday, December 22, 2013 10:49 AM

Thanks Clay,

Its  helped me  alot.


Monday, June 2, 2014 6:12 PM

It runs perfect... Thanks a ton.

Can we use an option "On error resume next"?


Friday, June 6, 2014 10:00 PM | 1 vote

It runs perfect... Thanks a ton.

Can we use an option "On error resume next"?

You can one set the ErrorAction parameter to SilentlyContinue, and also set the ErrorVariable so you can capture any errors and do something if one happens

Restart-Service -Name SomeService -ErrorAction SilentlyContinue -ErrorVariable myError

if ($myError)
{
  Do Something
}

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet


Thursday, May 25, 2017 10:03 AM

Hello Clayman2,

Is there anyway I can pass the server local creds in the above command?

Regards


Monday, July 24, 2017 4:15 PM

How would I output the servername and success/failed status for each?