#PSTip Starting and Stopping services on remote computers

Consider the following PowerShell 2.0 command:

Get-Service -Name wuauserv -ComputerName Server1 | Stop-Service

What do you think is happening here? You get the Windows Update service from Server1 and stop it, right? Wrong!
There’s a bug in PowerShell 2.0 and what actually happens is that Stop-Service stops the service on the local computer.

This has ben fixed in v3 but there’s a trick you can use to make it work in v2, pass the service instance to the InputObject Parameter:

PS> $svc = Get-Service -Name wuauserv -ComputerName Server1
PS> Stop-Service -InputObject $svc

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts