Home > PowerShell > PowerShell: Start and stop services on remote computer with alternate credentials

PowerShell: Start and stop services on remote computer with alternate credentials

I received a comment yesterday on most popular post of my blog(http://techibee.com/powershell/startstoprestart-service-on-remote-computer-with-powershell/693) asking for a way to start, stop, restart services using alternate credentials. I thought about it for some time and came up with below script.

Note that I am not going to use built-in cmdlets like get-service, start-service, stop-service in here since they don’t have an option to provide alternate credentials. Instead I am replying on WMI class Win32_Process which allows me to pass the credentials when I use through Get-WMIObject cmdlet.

Here is the code.

$credential = Get-Credential
$myservice = Get-WmiObject -Class Win32_Service -ComputerName SERVER1 ` -Credential $credential -Filter "Name='spooler'"
$myservice.stopservice()

It is that simple. Similarly if you want to start the service just use “startservice” method like given below.

$credential = Get-Credential
$myservice = Get-WmiObject -Class Win32_Service -ComputerName SERVER1 `
-Credential $credential -Filter "Name='spooler'"
$myservice.startservice()

Hope this helps and this article also gains some popularity.

 

 

 

 

  1. November 17, 2011 at 8:58 pm | #1

    An alternative, if you have PowerShell remoting enabled is to use Invoke-Command:

    invoke-command {Restart-Service spooler} -comp Server1-cred $credential

    This is especially handy if you want to manage a service on multiple machines.

  1. No trackbacks yet.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>