Welcome to MSDN Blogs Sign in | Join | Help

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

Published Monday, June 01, 2009 2:44 PM by ssehgal

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Configuring Windows services using Command Prompt | Microsoft Share Point

# re: Configuring Windows services using Command Prompt

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

Monday, August 24, 2009 12:41 PM by Alex

# re: Configuring Windows services using Command Prompt

Alex,

Use this syntax:

sc config "Service1" obj= "localsystem"

-Sidharth

Monday, August 24, 2009 2:35 PM by ssehgal

# re: Configuring Windows services using Command Prompt

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 ...

Monday, August 24, 2009 3:23 PM by Alex

# re: Configuring Windows services using Command Prompt

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.

Monday, August 24, 2009 4:32 PM by ssehgal

# re: Configuring Windows services using Command Prompt

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

Monday, August 24, 2009 4:45 PM by Alex

# re: Configuring Windows services using Command Prompt

I have not managed to correct the mistake ... you have some idea of the cause or possible solution?

Monday, August 24, 2009 5:34 PM by Alex

# re: Configuring Windows services using Command Prompt

the full error message is:

[SC] ChangueServiceConfig FAILED 1057

the account name is invalid or does not exist or the password is invalid for the specified account name

Alex Moreno

alexmoreno2@hotmail.com

Tuesday, August 25, 2009 10:14 AM by Alex

# re: Configuring Windows services using Command Prompt

already solved the problem, I had to add the sentence empty password ...

password = ""

Thank you!

Tuesday, August 25, 2009 10:30 AM by Alex

# re: Configuring Windows services using Command Prompt

Thats Great :)

For me it works fine even without specifying the password. We both seem to be running different versions of OS.

Tuesday, August 25, 2009 1:46 PM by ssehgal

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker