How to create a Windows service by using Sc.exe
Although you can use the Netsvc.exe and Instsrv.exe tools to start a service on a remote computer, these tools do not give you the ability to remotely create a service.
Note The Srvinstw.exe tool is the graphical user interface (GUI) version used to create remote services. Srvinstw.exe is not a command-line tool.
Sc.exe also allows you to call any of the service control API functions and vary any of the parameters from the command line. The advantage to this is that it provides a convenient way to create or configure the service information in the registry and the Service Control Manager database. You do not have to configure the service by manually creating entries in the registry and then restarting the computer to force the Service Control Manager to update its database.
Sc.exe uses the following syntax:
Syntax1 (use Syntax1 to run Sc.exe)
Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two backslash (\) characters (for example, \\myserver). To run Sc.exe on the local computer, do not supply this parameter.
Specifies the sc command. Note that many of the sc commands require administrative privileges on the specified computer. Sc.exe supports the following commands:Config
Changes the configuration of a service (persistent).
Sends a Continue control request to a service.
Sends a control to a service.
Creates a service (adds it to the registry).
Deletes a service (from the registry).
Enumerates service dependencies.
Obtains the DisplayName for a service.
Obtains the ServiceKeyName for a service.
Sends an Interrogate control request to a service.
Sends a Pause control request to a service.
Queries configuration for the service. For detailed information, see the reference section, "SC QC."
Queries the status for a service, or enumerates the status for types of services. For detailed information, see the reference section, "SC QUERY."
Starts a service
Sends a Stop request to a service.
Specifies the name given to the Service key in the registry. Note that this is different from the display name (which is what you see with net start command and the Services tool in Control Panel. Sc.exe uses the service key name as the primary identifier for the service.
The Optionname and Optionvalue parameters allow you to specify the names and values of optional command parameters. Note that there is no space between the Optionname and the equal sign. You can supply none, one, or more optional parameters name and value pairs.
Specifies the value for the parameter named by Optionname. The range of valid values is often restricted for each Optionname. For a list of available values, request help for each command.
The sc create command creates an entry for the service in the registry and in the Service Control Manager database.
Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two backslash characters (for example, \\myserver). To run SC on the local computer, do not supply this parameter.
Specifies the name given to the service key in the registry. Note that this is different from the display name, which is what you see with net start and the Services tool in Control Panel. Sc.exe uses the service key name as the primary identifier for the service.
The Optionname and Optionvalue parameters allow you to specify the names and values of optional parameters. Note that there is no space between the Optionname and the equal sign. You can supply none, one, or more optional parameters name and value pairs. The sc query command supports the following values:OptionnameOptionvaluetype=own, share, interact, kernel, filesys
Type of service you want to create. Optionvalues include types used by drivers.
(default = share)
start=boot, system, auto, demand, disabled
Start type for the service. Option values include types used by drivers.
(default = demand)
error=normal, severe, critical, ignore
Severity of error if the service does not start during boot.
(default = normal)
Path name to the service binary file. There is no default for this parameter. This string must be supplied.
Name of group which this service is a member of. The list of groups are stored in the registry under ServiceGroupOrder.
(default = nothing)
If this string is set to "yes," Sc.exe obtains a TagId from the CreateService call, however, Sc.exe does not display the tag.
(default = nothing)
depend=(space separated string)
Names of services or groups that must start before this service.
Name of account in which the service runs. For drivers, this is the Windows driver object name.
(default = LocalSystem)
A string that can be used by user-interface programs to identify the service.
A password string. This is required if an account other than LocalSystem is used.
Specifies the value for the parameter named by Optionname. See the Optionname reference for a list of supported values. When a string is to be input, the use of empty quotes means that an empty string is passed in.Note that there is a space between OptionValue and the equal sign.
Note The sc create command performs the operations of the CreateService API function.
The following example creates a registry entry for the service named "NewService" on the computer called \\remotecomputer:
This service is created as a WIN32_SHARE_PROCESS with a SERVICE_DEMAND_START start-type by default. It does not have any dependencies, and runs in the LocalSystem security context.
The following example creates the service on the local computer as an auto-start service that runs in its own process. It has dependencies on the TDI group and on the NetBIOS service. Notice that you must add quotes around the list of space-separated dependencies.
For further details about the Sc.exe tool, please see the Sc-dev.txt document in the Resource Kit. This document describes Sc.exe in greater detail.
Article ID: 251192 - Last Review: 09/11/2011 06:54:00 - Revision: 6.0
- kbhowto kbexpertiseinter kbinfo KB251192