Techibee.com

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

I received a comment yesterday on most popular post of my blog(https://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.

 

 

 

 

Exit mobile version