PowerShell: Quick and easy to start stop a remote service

by Sitaram Pamarthi on November 22, 2011

Do you like one liners in powershell? Here is the quick and easy way to start, stop, restart a service on remote computer. This doesn’t require PowerShell remoting. That means you can use it against any computer which has windows operating system installed.

So far I have authored two articles on managing services using powershell:

  1. Start/Stop/Restart service on remote computer with powershell
  2. PowerShell: Start and stop services on remote computer with alternate credentials

The first one I wrote when I was not matured enough with PowerShell and the second one recently to address a specific requirement where user need to pass alternate credentials to manage services.

As most system administrators love to use poweshell one-liners which avoids any external script/module invocation, I want to share this little one which starts, stops, and restarts a service on remote computer.

Start a service on remote computer:

Start-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

Stop a service on remote computer:

Stop-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

Restart a service on remote computer:

Restart-Service -InputObject $(Get-Service -Computer COMPUTER1 -Name spooler)

Hope these little ones helps.

This post was written by...

– who has written 495 posts on Techibee.com.

Sitaram Pamarthi is a windows administrators with 9+ years of experience.read more

{ 12 comments… read them below or add one }

Jeffery Hicks (MVP) November 23, 2011 at 7:13 pm

Very clever. Normally you would think you could run a command like:

get-services spooler -computer computerA | stop-service

but that won’t work. This is probably not as fluid as we might like but it gets the job done.

Reply

Sitaram Pamarthi November 23, 2011 at 9:02 pm

Thanks Jeffry Hicks for your comments!

Reply

Aditya June 11, 2012 at 2:03 pm

When I try to use above commands for mutiple remote computers it doesn’t work. Is there any syntax which can help for multiple machines service restart in a go.

Reply

Sitaram Pamarthi June 12, 2012 at 8:41 pm

Aditya, try this to pick computers name from text file and stop service remotely.

Get-Content c:\comps.txt | % {
Stop-Service -InputObject $(Get-Service -Computer $_ -Name spooler)
}

Reply

Anonymous June 21, 2012 at 9:39 pm

Thank you so much for this, i was trying to find a way to use PS instead of the wmi cmdlts. VERY Helpful!

Reply

Alan Kaplan September 14, 2012 at 9:19 pm

What if there is a dependency?

Reply

GuyThomas January 20, 2013 at 4:48 pm

Get-Service -ComputerName victim # Fails
###
Enter-PSSession -ComputerName victim -Credential victim\admin
get-service # This works
###
Question is Get-Service -computername flaky?

Reply

Sitaram Pamarthi January 21, 2013 at 10:50 am

When you are doing Enter-PSSession you are passing explicit credentials which what you are not doing when using Get-Service. Try passing the credentials to it and I should work.

Reply

Lee Busksey April 6, 2013 at 6:45 pm

Doesn’t work for me. I am logged in with domain admin right,and this is what I get..

PS H:\> type vmtools.ps1
Start-Service -InputObject $(Get-Service -Computer cdf2-test-w08 -Name VMtools)

PS H:\> .\vmtools.ps1
Get-Service : Cannot find any service with service name ‘VMtools’.
At H:\vmtools.ps1:1 char:30
+ Start-Service -InputObject $(Get-Service -Computer cdf2-test-w08 -Name VMtools)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (VMtools:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

Start-Service : Cannot validate argument on parameter ‘InputObject’. The argument is null or empty. Supply an argument
that is not null or empty and then try the command again.
At H:\vmtools.ps1:1 char:28
+ Start-Service -InputObject $(Get-Service -Computer cdf2-test-w08 -Name VMtools)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartServiceCommand

Reply

Sitaram Pamarthi April 6, 2013 at 9:00 pm

The error message is pretty clear. There is no service with the name “VMtools” on “cdf2-test-w08″ computer. You might want to cross check that tools are installed or not.

Reply

lakshmi December 20, 2013 at 4:59 pm

how to start a list of services on one computer using this and how to check the same(means whether it is started or not)

Thanks in advance

Reply

Sitaram Pamarthi December 29, 2013 at 4:27 pm

Lakshmi, understood the requirement. I will come-up with a script soon. It is already in my to do list.

Reply

Leave a Comment

{ 3 trackbacks }

Previous post:

Next post: