48

Using sc command we can query, start , stop windows services.
For ex:

sc query "windows service name"

The sc config command changes the configuration of the service, but I don't know how to use it.

Could someone tell me how we can set the username and password for any windows service?

Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
sundar venugopal
  • 3,080
  • 6
  • 39
  • 45

2 Answers2

101

This works:

sc.exe config "[servicename]" obj= "[.\username]" password= "[password]"

Where each of the [bracketed] items are replaced with the true arguments. (Keep the quotes, but don't keep the brackets.)

Just keep in mind that:

  • The spacing in the above example matters. obj= "foo" is correct; obj="foo" is not.
  • '.' is an alias to the local machine, you can specify a domain there (or your local computer name) if you wish.
  • Passwords aren't validated until the service is started
  • Quote your parameters, as above. You can sometimes get by without quotes, but good luck.
Jude Allred
  • 10,977
  • 7
  • 28
  • 27
Andrew Ferrier
  • 16,664
  • 13
  • 47
  • 76
  • 7
    Andrew , it worked with minor changes. obj= "domain\username" obj= "LocalSystem" we have to have space inbetween obj= and username. userid's are validated that is good. we have to have blank space in between password= and actual password. password are not validated, that is bit of pain. to set back the service again to LocalSystem, which do have passsword, we have give some dummy password to make it work. like sc config "servicename" obj= "LocalSystem" password= "notused" – sundar venugopal Nov 21 '08 at 10:49
  • 1
    When I tried running this command in PowerShell, I got errors like: "Set-Content : A positional parameter cannot be found that accepts argument 'obj='." I worked around it by using CMD: `cmd.exe -e "sc config Service obj= user password= pass"` – Jeremy Nov 22 '11 at 00:32
  • 3
    This does not work either. The SC config *service* obj= "*service name*" password= "*password*" does not correctly set the password -- thus, the service cannot be started. – Grayson Jan 18 '13 at 22:35
  • 2
    @Jeremy In powershell, "sc" is an alias to Set-Content. Use `& sc.exe` instead and there won't be a conflict. – Jude Allred Feb 03 '14 at 19:31
  • 5
    If the service has "interact with desktop" set already, you'll get an error code 87. To get around this: `sc.exe config "ServiceName" obj= "USER@DOMAIN" password= "MyPassword" TYPE= own` (https://support.microsoft.com/en-us/kb/264663) – Chris S Mar 22 '16 at 16:04
  • I always get logon failure when setting password through cmd. When I manually go have add the same password it works. No special characters in my password and I'm using double quotes as well – Omair Nabiel Jul 09 '20 at 00:04
  • 1
    @OmairNabiel as Rich K says in this thread https://stackoverflow.com/questions/14408973/using-sc-exe-to-set-service-credentials-password-fails "When you configure a service to run under a specific account via the normal route from the service properties windows automatically grants the account the log in as service right. When you use sc.exe you also have to grant the user the log on as service right." – Mark Willis May 11 '21 at 13:43
9

In PowerShell, the "sc" command is an alias for the Set-Content cmdlet. You can workaround this using the following syntax:

sc.exe config Service obj= user password= pass

Specyfying the .exe extension, PowerShell bypasses the alias lookup.

HTH

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • It doesn't work for me. I wonder if this method support other characters different than alphanumeric. – Maverick Aug 26 '13 at 12:18
  • Can also just whack a "cmd /c" in front of the sc command line which makes PowerShell run it in CMD – KERR Aug 13 '21 at 03:56