TExcelTAD
Home › Windows CMD & Batch › sc: Windows Services

sc: Windows Services

A service is a program that Windows starts in the background, usually at boot, without anyone logging in: the print spooler, Windows Update, the DHCP client, a database server, an SSH server. Services are started and supervised by the Service Control Manager (SCM), and sc.exe is the command-line client for it. (How services fit into Windows as a whole is covered in Windows Services in the OS course.)

text
 sc.exe / net / services.msc / PowerShell
                 |
                 v
     Service Control Manager (services.exe)      config in the registry:
                 |                               HKLM\SYSTEM\CurrentControlSet\Services\<name>
     +-----------+-----------+
     v           v           v
 spoolsv.exe  svchost.exe  sshd.exe ...          (each service runs as LocalSystem,
              (hosts many                          LocalService, NetworkService
               small services)                     or a named account)

Every service has two names: a short service name (Spooler, wuauserv) used by commands, and a display name (Print Spooler, Windows Update) shown in the Services window (services.msc). sc always wants the short one.

Note: in Windows PowerShell 5.1 (the built-in powershell.exe), sc is an alias for Set-Content! PowerShell 7 dropped that alias, but typing sc.exe works in every shell. In Command Prompt plain sc is fine.

Querying a service

cmd
C:\Users\ada>sc query Spooler

SERVICE_NAME: Spooler
        TYPE               : 110  WIN32_OWN_PROCESS  (interactive)
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Sample output. The interesting lines:

Field Meaning
TYPE WIN32_OWN_PROCESS (its own .exe) or WIN32_SHARE_PROCESS (shares an svchost.exe with other services); KERNEL_DRIVER for drivers
STATE 1 STOPPED, 2 START_PENDING, 3 STOP_PENDING, 4 RUNNING, 7 PAUSED (5 and 6 are continue/pause pending)
the flags line what the service accepts: stopping, pausing, shutdown notification
WIN32_EXIT_CODE the error code of the last stop; 1077 means "never started since boot"
CHECKPOINT, WAIT_HINT progress counters while a service is starting or stopping

sc queryex adds the PID (match it with tasklist, see tasklist & taskkill). sc qc ("query config") shows how the service is set up:

cmd
C:\Users\ada>sc qc Spooler
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: Spooler
        TYPE               : 110  WIN32_OWN_PROCESS  (interactive)
        START_TYPE         : 2   AUTO_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : C:\Windows\System32\spoolsv.exe
        LOAD_ORDER_GROUP   : SpoolerGroup
        TAG                : 0
        DISPLAY_NAME       : Print Spooler
        DEPENDENCIES       : RPCSS
                           : http
        SERVICE_START_NAME : LocalSystem

START_TYPE is AUTO_START (at boot), DEMAND_START (manual: when something asks for it) or DISABLED; DEPENDENCIES must be running first; SERVICE_START_NAME is the account it runs as.

Listing services

Plain sc query lists only running services. Add state= all (or state= inactive), and mind the odd syntax: the option name includes the = and needs a space after it:

cmd
C:\Users\ada>sc query state= all | findstr /c:"SERVICE_NAME" /c:"STATE"
SERVICE_NAME: AarSvc_5f2a1
        STATE              : 1  STOPPED
SERVICE_NAME: Appinfo
        STATE              : 4  RUNNING
...

sc query state=all (no space) fails with a usage message. sc queryex type= service state= all is the full form.

Turning sc output into a table

The playground has no Service Control Manager, so the next examples save sc query text to a file and parse it. On a real PC replace services.txt with ('sc query state^= all') in the for /f.

The plan: remember the name from each SERVICE_NAME: line, and when the STATE line of the same block arrives, print both. Token 4 of the STATE line is the state word:

cmd · try it yourself
@echo off
setlocal enabledelayedexpansion
(
echo SERVICE_NAME: Spooler
echo         TYPE               : 110  WIN32_OWN_PROCESS  ^(interactive^)
echo         STATE              : 4  RUNNING
echo SERVICE_NAME: W32Time
echo         TYPE               : 30  WIN32
echo         STATE              : 1  STOPPED
echo SERVICE_NAME: wuauserv
echo         TYPE               : 30  WIN32
echo         STATE              : 2  START_PENDING
echo SERVICE_NAME: sshd
echo         TYPE               : 10  WIN32_OWN_PROCESS
echo         STATE              : 4  RUNNING
) > services.txt

set /a running=0, other=0
for /f "tokens=1,2,3,4" %%a in (services.txt) do (
  if "%%a"=="SERVICE_NAME:" set "name=%%b"
  if "%%a"=="STATE" (
    set "line=!name!                "
    echo !line:~0,12! %%d
    if "%%d"=="RUNNING" (set /a running+=1) else (set /a other+=1)
  )
)
echo %running% running, %other% not running
Spooler      RUNNING
W32Time      STOPPED
wuauserv     START_PENDING
sshd         RUNNING
2 running, 2 not running

Padding with spaces and cutting with !line:~0,12! makes a fixed-width column (see String Manipulation).

Reading the configuration works the same way. tokens=1,* delims=: splits at the first colon only, so the program path C:\Windows\... keeps its own colon:

cmd · try it yourself
@echo off
setlocal enabledelayedexpansion
(
echo SERVICE_NAME: Spooler
echo         START_TYPE         : 2   AUTO_START
echo         BINARY_PATH_NAME   : C:\Windows\System32\spoolsv.exe
echo         DISPLAY_NAME       : Print Spooler
echo         SERVICE_START_NAME : LocalSystem
) > qc.txt

for /f "tokens=1,* delims=:" %%a in (qc.txt) do (
  for /f "tokens=*" %%k in ("%%a") do set "key=%%k"
  for /f "tokens=*" %%v in ("%%b") do set "val=%%v"
  set "key=!key: =!"
  set "cfg_!key!=!val!"
)
echo Display name: %cfg_DISPLAY_NAME%
echo Program:      %cfg_BINARY_PATH_NAME%
echo Runs as:      %cfg_SERVICE_START_NAME%
for /f "tokens=2" %%s in ("%cfg_START_TYPE%") do set "start=%%s"
if "%start%"=="AUTO_START" (echo Starts with Windows) else (echo Start type: %start%)
Display name: Print Spooler
Program:      C:\Windows\System32\spoolsv.exe
Runs as:      LocalSystem
Starts with Windows

Each line becomes a variable named after its key (cfg_DISPLAY_NAME, cfg_START_TYPE, …): a handy way to load any key : value text into batch variables.

Starting, stopping and configuring

Changing services requires an elevated prompt. Without it every change fails with error 5:

cmd
C:\Users\ada>sc stop Spooler
[SC] OpenService FAILED 5:

Access is denied.

In an Administrator prompt (sample output):

cmd
C:\Windows\System32>sc stop Spooler

SERVICE_NAME: Spooler
        TYPE               : 110  WIN32_OWN_PROCESS  (interactive)
        STATE              : 3  STOP_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

C:\Windows\System32>sc start Spooler

SERVICE_NAME: Spooler
        TYPE               : 110  WIN32_OWN_PROCESS  (interactive)
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x7d0
        PID                : 9284
        FLAGS              :

C:\Windows\System32>sc config Spooler start= demand
[SC] ChangeServiceConfig SUCCESS

C:\Windows\System32>sc start NoSuchService
[SC] StartService: OpenService FAILED 1060:

The specified service does not exist as an installed service.

Note the _PENDING states: sc doesn't wait. It sends the request and returns while the service is still starting or stopping. A script that stops and immediately restarts a service may get error 1056 ("An instance of the service is already running") or 1061 ("The service cannot accept control messages at this time"). Either loop on sc query until the state is what you want, or use net start / net stop, which wait for the result:

cmd
C:\Windows\System32>net stop spooler
The Print Spooler service is stopping.
The Print Spooler service was stopped successfully.

C:\Windows\System32>net start spooler
The Print Spooler service is starting.
The Print Spooler service was started successfully.

The most used sc sub-commands:

Command Does
sc query name / sc queryex name state (+ PID)
sc qc name configuration
sc start name / sc stop name start / stop (asynchronous)
sc config name start= auto|delayed-auto|demand|disabled start type
sc description name "text" set the description
sc failure name reset= 86400 actions= restart/60000 restart automatically 60 s after a crash
sc create name binPath= "C:\app\svc.exe" start= auto register a new service (the program must be written as a service)
sc delete name remove it (after the next stop)
sc \\server query name the same on a remote computer

PowerShell equivalents

CMD PowerShell
sc query Spooler Get-Service Spooler
sc query state= all Get-Service
net start spooler Start-Service Spooler
net stop spooler Stop-Service Spooler
sc config Spooler start= demand Set-Service Spooler -StartupType Manual
sc qc Spooler Get-CimInstance Win32_Service -Filter "Name='Spooler'"
powershell
PS C:\Users\ada> Get-Service Spooler, W32Time, wuauserv

Status   Name               DisplayName
------   ----               -----------
Running  Spooler            Print Spooler
Stopped  W32Time            Windows Time
Running  wuauserv           Windows Update

Sample output. The Linux counterpart of all this is systemctl (cron & systemd, systemd).

Test yourself

Which command changes the Spooler service to start manually?

  1. sc config Spooler start=demand
  2. sc config Spooler start= demand
  3. sc set Spooler manual
  4. sc start Spooler /manual

sc stop MyService returned immediately with STATE : 3 STOP_PENDING. What does that mean?

  1. The stop failed
  2. The stop request was accepted, but the service hasn't finished stopping yet
  3. The service is paused
  4. The service will stop at the next reboot

In Windows PowerShell 5.1 you type sc query Spooler and no service information appears. Why?

  1. sc only works on Windows Server
  2. There sc is an alias for Set-Content; type sc.exe instead
  3. The service name must be quoted
  4. PowerShell needs sc /query