Working with remote Windows services in PowerShell

Here’s how to list, start and stop Windows services on a remote computer using PowerShell:

[reflection.assembly]::loadwithpartialname("System.ServiceProcess")

# List all services on the remote computer
[System.ServiceProcess.ServiceController]::GetServices('10.123.123.125')

# Stop the service and wait for it to change status
$service = (new-object System.ServiceProcess.ServiceController('Service name','10.123.123.125'))
$service.WaitForStatus('Stopped',(new-timespan -seconds 5))
$service.Stop()

# Start the service again
$service.Start()

This can come in really handy in deployment scripts, etc.

(Parts of this post came from The PowerShell Guy).

/Emil

Update: In the earlier version of this post I used new-object every time I needed a reference to the service. That’s not necessary, it’s much prettier to use a variable for the reference, so I have now updated the code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.