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:
- Start/Stop/Restart service on remote computer with powershell
- 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.
Comments on this entry are closed.
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.
Thanks Jeffry Hicks for your comments!
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.
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)
}
Thank you so much for this, i was trying to find a way to use PS instead of the wmi cmdlts. VERY Helpful!
What if there is a dependency?
Get-Service -ComputerName victim # Fails
###
Enter-PSSession -ComputerName victim -Credential victim\admin
get-service # This works
###
Question is Get-Service -computername flaky?
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.
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
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.
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
Lakshmi, understood the requirement. I will come-up with a script soon. It is already in my to do list.