Configuring Windows services using Command Prompt

Configuring the properties of a Windows Service using command prompt and scripting it in a batch file is really simple and can save you from performing the same manual configuration again and again.

The sc utility comes handy to achieve this.

In the example below, we are going to configure a windows service to:

  • Set the startup type as automatic.
  • Run the service under a particular account
  • Set the failure actions for the service and set the reset period.
  • Define dependencies.

To give a more clear understanding, screenshots of the service properties UI is attached along with the scripts to set the configurations.

1. Set the startup type as automatic.

image

sc config "Service1"  start= auto

Note here that although the Display name of the Service is myfirstservice, The actual Service name is Service1. We need to use the actual service name in the scripts to control the service properties.

2. Run the service under a particular account

image 

sc config "Service1" obj= mydomain\sidharth password= MyPassword

 

3.Set the failure actions for the service and set the reset period.

image 

sc failure "Service1"  actions= restart/180000/restart/180000/""/180000 reset= 86400

The restart service times are in milliseconds and the reset fail count time is in seconds.

4. Define dependencies.

image

Our service depends on the SQL Server service, whose Actual Service name is MSSQLServer, To set this dependency, use the following command.

sc config "Service1"  depend= "MSSQLServer"

Finally, to start the server use

net start Service1

We can also club all the command and save it as a batch file, it will look like this:

sc config "Service1"  start= auto sc config "Service1" obj= mydomain\sidharth password= MyPassword sc failure "Service1"  actions= restart/180000/restart/180000/""/180000 reset= 86400 sc config "Service1"  depend= "MSSQLServer" net start Service1

Comments

  • Anonymous
    June 01, 2009
    PingBack from http://microsoft-sharepoint.simplynetdev.com/configuring-windows-services-using-command-prompt/

  • Anonymous
    August 24, 2009
    I desire that the service is activated with a local account and not with a particular account, which would be the syntax to follow? Thank you! alexmoreno2@hotmail.com Venezuela

  • Anonymous
    August 24, 2009
    Alex, Use this syntax: sc config "Service1" obj= "localsystem" -Sidharth

  • Anonymous
    August 24, 2009
    thank you very much Sidharth for your quick response ... I tried it with this syntax, but I get an following error : [SC] ChangueServiceConfig FAILED 1057 I hope you can help me ...

  • Anonymous
    August 24, 2009
    Alex, It must be giving a detailed description of failure also. Can you please check and send it across. Make sure there is no space in localsystem, copy the above snippet as it is and replace Service1 by your service name.

  • Anonymous
    August 24, 2009
    I substituted Service1 by the name of my service, and LocalSystem has no spaces, I really do not know the cause of this error. Thanks

  • Anonymous
    August 24, 2009
    I have not managed to correct the mistake ... you have some idea of the cause or possible solution?

  • Anonymous
    August 25, 2009
    The comment has been removed

  • Anonymous
    August 25, 2009
    already solved the problem, I had to add the sentence empty password ... password = "" Thank you!

  • Anonymous
    August 25, 2009
    Thats Great :) For me it works fine even without specifying the password. We both seem to be running different versions of OS.

  • Anonymous
    December 30, 2010
    You should use this command when changing back to Local System: sc config "servicename" obj= LocalSystem password= LocalSystem

  • Anonymous
    February 13, 2011
    Do blank passwords work for NON LocalSystem users? I need to install a JBoss service as the current logged used on a Windows XP Professional. I set the user and password with this command in my automated install BAT script: sc config RFBCAJAJBOSS obj= ".%USERNAME%" password= "" %USERNAME% has a blank password and does autologon on the PC I also set the registry key to allow for blank passwords on services: [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlLsa] "limitblankpassworduse"=dword:00000000 BUT ID DOES NOT WORK:

  • The user is set correctly on the service, but the password is either ignored or changed by something else (non blank) because after running my install.BAT the service DOES NOT START.
  • You have to MANUALLY go to the Services control panel (under Start-> Control Panel -> Sys admin tools -> Services) and select the login tab ensuring to set a blank password for the user. Is there anyway to automate this and avoid the installation process to include this "manual password blanking" step?
  • Anonymous
    June 22, 2012
    sc config SQL Server (MSSQLSERVER) obj= .username password= password type= own tells me the service does not exist but all other 7 services do work, i have tried it with and with out the quotation marks. please Help me !! Aptacnik@aperio.com

  • Anonymous
    June 25, 2012
    Hi Allan, You have to use the Service Name (can be obtained by right clicking the service > properties > Service name). For you, it will be MSSQLSERVER, so the following command should work: sc config "MSSQLSERVER" obj= .username password= password Thanks, Sidharth