Creating a Windows Service using the command line with the sc command is a fundamental skill for system administrators and developers who need to deploy background processes without a graphical interface. The sc command, short for Service Control, provides a powerful and flexible way to interact with the Service Control Manager, allowing for the installation, configuration, and management of services directly from the terminal.
Understanding the SC Command
The sc command is a native Windows utility that communicates directly with the Service Control Manager (SCM). Unlike the more common "Services" Microsoft Management Console (MMC), sc operates in a text-based environment, making it ideal for scripting, remote administration via PowerShell, and scenarios where the graphical desktop is unavailable. At its core, the sc command syntax follows a straightforward pattern: sc [ServerName] [Command] [ServiceName] [Option].
Preparing the Executable
Before you can create a service, you need an executable file (.exe) that is designed to run as a service. This application must be able to handle service-specific control requests, such as stopping or pausing. It should include a main loop and respond to service control signals sent by the SCM. For testing purposes, you can create a simple console application that logs its activity or use an existing robust application that supports service mode installation.
Basic Service Creation with SC Create
To install a new service, you use the `create` subcommand. This action registers the executable with the SCM, defining how the service should be started. The most common syntax requires the service name, the binary path, and the start type. The start type determines when the service launches, such as automatically during boot or manually by a user. Here is the basic structure of the command:
sc create "ServiceName" binPath= "C:\Path\To\YourExecutable.exe" start= auto
It is critical to note the spacing in this command. There must be a space after the equals sign for the binPath and start parameters, and the service name should be enclosed in quotes if it contains spaces.
Configuring Service Dependencies and Credentials
Advanced service creation often requires specifying dependencies or the account under which the service runs. By default, services run under the Local System account, which has broad permissions. For security best practices, you might want to run the service under a specific user account. You can achieve this by adding the `obj=` parameter. Furthermore, if your service relies on other services, you can define these dependencies using the `depend=` parameter to ensure the SCM starts them in the correct order.
Managing the Service Lifecycle
Once the service is created, you can manage its state using other sc commands. To start the service immediately after creation, you would use `sc start "ServiceName"`. If you need to stop it temporarily, the command is `sc stop "ServiceName"`. For troubleshooting, you can query the service status and configuration using `sc qc "ServiceName"` to view the startup type and binary path, or `sc query "ServiceName"` to see if it is currently running or stopped.
Removing a Service
If the service is no longer needed, it should be removed cleanly to avoid cluttering the Service Control Manager. This is done using the `delete` command. The process is not instantaneous; the SCM marks the service for deletion during the next restart of the computer. To remove a service, execute the following command:
sc delete "ServiceName"