How to use the command 'sc' (with examples)
- Windows
- December 25, 2023
The ‘sc’ command in Windows allows users to communicate with the Service Control Manager and services. It provides various functionalities, such as querying the status of a service, starting or stopping a service asynchronously, and configuring the type of a service.
Use case 1: Show the status of a service
Code:
sc.exe query service_name
Motivation: This use case can be useful when you want to check the current status of a specific service on your Windows machine. By using the ‘sc’ command to query the service, you can quickly determine whether the service is running or not.
Explanation:
sc.exe
: the executable file for the ‘sc’ command.query
: the argument used to specify that we want to query the service.service_name
: the name of the service you want to check the status for. If no service name is provided, all services will be listed.
Example output:
SERVICE_NAME: Windows Update
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
Use case 2: Start a service asynchronously
Code:
sc.exe start service_name
Motivation: When you need to start a specific service on your Windows machine, using the ‘sc’ command to start the service asynchronously can be useful. Asynchronous starting allows the command to return immediately without waiting for the service to fully start, making it faster than synchronous starting.
Explanation:
sc.exe
: the executable file for the ‘sc’ command.start
: the argument used to specify that we want to start the service.service_name
: the name of the service you want to start.
Example output:
[SC] StartService FAILED 1058:
The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.
Use case 3: Stop a service asynchronously
Code:
sc.exe stop service_name
Motivation: Sometimes, you may need to stop a specific service on your Windows machine. The ‘sc’ command can be used to stop the service asynchronously, allowing the command to return immediately without waiting for the service to fully stop. This can be beneficial when you want to perform other tasks while the service is being stopped.
Explanation:
sc.exe
: the executable file for the ‘sc’ command.stop
: the argument used to specify that we want to stop the service.service_name
: the name of the service you want to stop.
Example output:
SERVICE_NAME: Windows Update
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 3 STOP_PENDING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x3
WAIT_HINT : 0x7530
Use case 4: Set the type of a service
Code:
sc.exe config service_name type= service_type
Motivation: The ‘sc’ command can also be used to change the type of a specific service on your Windows machine. This can be useful when you want to modify the behavior of a service, such as changing it from an interactive service to a non-interactive service or vice versa.
Explanation:
sc.exe
: the executable file for the ‘sc’ command.config
: the argument used to specify that we want to configure the service.service_name
: the name of the service you want to configure.type
: the argument used to specify the type subcommand for configuring the service.service_type
: the type of the service. It can be one of the following values: KERNEL_DRIVER, FILE_SYSTEM_DRIVER, ADAPTER, RECOGNIZER_DRIVER, WIN32_OWN_PROCESS, WIN32_SHARE_PROCESS, INTERACTIVE_PROCESS.
Example output:
[SC] ChangeServiceConfig SUCCESS
Conclusion
The ‘sc’ command in Windows provides a convenient way to communicate with the Service Control Manager and services. Whether you need to check the status of a service, start or stop a service asynchronously, or configure the type of a service, the ‘sc’ command offers the necessary functionalities. By understanding how to use the different use cases of the ‘sc’ command, you can effectively manage and control services on your Windows machine.