Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to execute the SC command against a network target as a different user. I need to be able to specify username and password on the commandline as well, as this is for scripting purposes. However, I see no options on SC for executing a command as another user. I see the 'obj' option which accepts a username, but that appears to be for setting the user account under which the service itself should run under.

If there is no way to change the user-context of the execution, is there another command that will allow me to create/start/stop a windows service in a scriptable manner (calling from TeamCity)

share|improve this question
Are the to machines joined to the same domain? – Mathias R. Jessen Feb 10 '12 at 23:24
@MathiasR.Jessen Yes, would be supplying domain\username as the username ideally – CoolUserName Feb 10 '12 at 23:27

Use the "RunAs" commandline tool, it lets you supply an explicit credential set, and supports both "Domain\Username" and UPN formatting, allowing you to authenticate as another user and the run your command:

   Runas /user:Domain\user "sc \\remoteServer stop serviceKeyName"


Set up a scheduled task with a specific logon account

share|improve this answer
Unfortunately this command does not allow one to specify the password in the command line, it can only be provided when prompted after execution. Hence, is not scriptable. – CoolUserName Feb 13 '12 at 16:05
Maybe useful to change windows service credential in remote Server. – Kiquenet Oct 16 '14 at 21:27
runAs tool similar in SysInternals ? Who's support that tool ? – Kiquenet Oct 16 '14 at 21:29

Ended up installing and using PsExec - Allows for inline specification of username/password, so is scriptable.

share|improve this answer
Works Psexec to change windows service credential in remote Server ? – Kiquenet Oct 16 '14 at 21:28

In PowerShell:

(get-wmiobject win32_service -comp servername -cred domain\user -filter "name='browser'").stopservice()

-cred will pop up a credentials dialog box also

Replace servername, domain\user and the name= with proper values

with -cred you can even used stored credentials.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.