TExcelTAD
Home › Operating Systems: Linux & Windows › Windows Services

Windows Services

A service is a program that runs in the background without a user being signed in: the print spooler, Windows Update, the DHCP client, a database server, an SSH server. Services are the Windows counterpart of Linux daemons managed by systemd. This lesson explains who starts them, how they are configured, why Task Manager shows dozens of svchost.exe processes, and how to manage them from CMD and PowerShell.

The Service Control Manager

The Service Control Manager (SCM) is services.exe, started by wininit.exe during boot. It:

  • reads the service database from the registry (HKLM\SYSTEM\CurrentControlSet\Services\<name>),
  • starts the services marked automatic, in dependency order,
  • launches each one under its configured account,
  • passes control requests (start, stop, pause, continue) from tools to services,
  • applies recovery actions when a service crashes (restart it, run a program, reboot).

A service program is an ordinary executable that, when it starts, connects back to the SCM (StartServiceCtrlDispatcher) and registers a handler for control requests. Kernel drivers are managed by the same database, with a different type.

Service configuration

Setting Values Meaning
Start type Boot, System drivers loaded by winload.efi / by the kernel during startup
Automatic started by the SCM at boot
Automatic (Delayed Start) started about two minutes after the other automatic services, to make sign-in faster
Manual started on demand (by a program, a dependency or a trigger)
Disabled cannot be started
Type own process, shared process its own .exe, or a DLL hosted inside svchost.exe
Account LocalSystem, LocalService, NetworkService, NT SERVICE\<name>, a user the security identity it runs as
Dependencies other services or groups must be running before this one starts
Recovery first, second, subsequent failure actions restart, run a program, reboot

Trigger-start services are a modern refinement of manual: the SCM starts them when an event happens, for example when a device is plugged in, an IP address appears or a group policy changes. Many Windows services are trigger-started so that nothing runs when it isn't needed.

The service accounts

Account Local rights On the network appears as
LocalSystem (NT AUTHORITY\SYSTEM) everything the computer account (DOMAIN\PC$)
LocalService limited, like a normal user anonymous
NetworkService limited the computer account
Virtual account (NT SERVICE\MyService) limited, its own SID for ACLs the computer account

Use the least privileged account that works; a bug in a LocalSystem service gives an attacker the whole machine. On Linux the same advice reads: run daemons as a dedicated system user, not as root.

Looking at services

From CMD, sc talks to the SCM directly (sample output — yours will differ):

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

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

Note: In PowerShell, sc is an alias for Set-Content! Type sc.exe there, or use the cmdlets below.

PowerShell has cmdlets that return objects you can filter and sort:

powershell
PS C:\Users\ada> Get-Service -Name Spooler, wuauserv, sshd | Format-Table Name, Status, StartType

Name     Status  StartType
----     ------  ---------
Spooler  Running Automatic
wuauserv Stopped    Manual
sshd     Running Automatic

PS C:\Users\ada> (Get-Service Spooler).ServicesDependedOn | Select-Object Name

Name
----
RPCSS
http

PS C:\Users\ada> Get-CimInstance Win32_Service -Filter "Name='Spooler'" |
>>   Select-Object Name, ProcessId, StartName, PathName

Name    ProcessId StartName   PathName
----    --------- ---------   --------
Spooler      3288 LocalSystem C:\Windows\System32\spoolsv.exe

Changing services needs an elevated (Administrator) prompt:

powershell
PS C:\Windows\system32> Stop-Service Spooler
PS C:\Windows\system32> Set-Service Spooler -StartupType Manual
PS C:\Windows\system32> Start-Service Spooler
PS C:\Windows\system32> New-Service -Name TadAgent -BinaryPathName 'C:\Tools\tad-agent.exe' -StartupType Automatic

The graphical tool is services.msc; Task Manager's Services tab shows the same list with PIDs.

svchost.exe: one host, many services

Many Windows services are not .exe files but DLLs. svchost.exe ("service host") is a generic process that loads one or more of these DLLs and runs them. Its command line names a group, svchost.exe -k netsvcs -p, and the registry says which DLL each service lives in (...\Services\<name>\Parameters\ServiceDll).

On older Windows versions dozens of services shared a handful of svchost processes to save memory. Since Windows 10 version 1703, on machines with more than about 3.5 GB of RAM, most services get their own svchost.exe. That costs a little memory but means a crash takes down one service, not twenty, and Task Manager can show which service uses the CPU.

cmd
C:\Users\ada>tasklist /svc /fi "imagename eq svchost.exe"

Image Name                     PID Services
========================= ======== ============================================
svchost.exe                   1100 BrokerInfrastructure, DcomLaunch, PlugPlay,
                                   Power, SystemEventsBroker
svchost.exe                   1196 RpcEptMapper, RpcSs
svchost.exe                   1288 LSM
svchost.exe                   1460 Dhcp
svchost.exe                   1472 EventLog
...

Start order: dependencies

A service can depend on other services. The SCM must start every dependency before the service itself, and when you stop a service, Windows first stops the services that depend on it (Stop-Service -Force). That is a topological sort of the dependency graph. This model reproduces it, including the error for a dependency cycle:

python · try it yourself
deps = {
    "Spooler":  ["RpcSs", "HTTP"],
    "RpcSs":    ["RpcEptMapper", "DcomLaunch"],
    "RpcEptMapper": [],
    "DcomLaunch": [],
    "HTTP":     [],
    "Dhcp":     ["Tcpip", "Afd"],
    "Tcpip":    [],
    "Afd":      [],
    "TadAgent": ["Dhcp", "Spooler"],
}

def start_order(deps):
    order, state = [], {}          # state: 1 = visiting, 2 = started
    def visit(name, path):
        if state.get(name) == 2:
            return
        if state.get(name) == 1:
            raise ValueError("dependency cycle: " + " -> ".join(path + [name]))
        state[name] = 1
        for d in sorted(deps.get(name, [])):
            visit(d, path + [name])
        state[name] = 2
        order.append(name)
    for name in sorted(deps):
        visit(name, [])
    return order

order = start_order(deps)
print("start:", ", ".join(order))
print("stop: ", ", ".join(reversed(order)))

deps["DcomLaunch"] = ["TadAgent"]          # a configuration mistake
try:
    start_order(deps)
except ValueError as e:
    print(e)
start: Afd, DcomLaunch, Tcpip, Dhcp, HTTP, RpcEptMapper, RpcSs, Spooler, TadAgent
stop:  TadAgent, Spooler, RpcSs, RpcEptMapper, HTTP, Dhcp, Tcpip, DcomLaunch, Afd
dependency cycle: DcomLaunch -> TadAgent -> Spooler -> RpcSs -> DcomLaunch

Stopping in reverse start order guarantees nothing is stopped while something still needs it. systemd does the same with Requires=/After= (see init & systemd).

Service states and crashes

A service moves through states that the SCM tracks: STOPPED → START_PENDING → RUNNING → STOP_PENDING → STOPPED (plus PAUSED). If a service process exits without reporting STOPPED, the SCM logs event 7031 or 7034 in the System log and runs the recovery actions. The default for many services is "restart after 1 minute" for the first two failures and "take no action" afterwards, so a crash loop doesn't eat the machine.

python · try it yourself
recovery = ["restart", "restart", "none"]     # first, second, subsequent failures

def run(service, crashes):
    failures = 0
    print(f"{service}: START_PENDING -> RUNNING")
    for minute in crashes:
        action = recovery[min(failures, len(recovery) - 1)]
        failures += 1
        print(f"  t={minute}m crash #{failures} -> action: {action}")
        if action == "none":
            print(f"{service}: STOPPED (no more restarts)")
            return
        print(f"{service}: START_PENDING -> RUNNING")

run("TadAgent", [5, 9, 12])
TadAgent: START_PENDING -> RUNNING
  t=5m crash #1 -> action: restart
TadAgent: START_PENDING -> RUNNING
  t=9m crash #2 -> action: restart
TadAgent: START_PENDING -> RUNNING
  t=12m crash #3 -> action: none
TadAgent: STOPPED (no more restarts)

Windows services vs systemd

Windows Linux (systemd)
Manager services.exe (SCM) systemd (PID 1)
Definition registry key under ...\Services unit file in /etc/systemd/system
Shared host process svchost.exe none (one process per unit)
List Get-Service, sc query systemctl list-units --type=service
Start/stop Start-Service, sc start systemctl start
Boot start Automatic / Delayed systemctl enable (WantedBy=multi-user.target)
Logs Event Viewer (System log) journalctl -u name
Restart on crash Recovery tab Restart=on-failure

Learn more: The Windows Registry · init & systemd · Windows NT Architecture

Test yourself

Which process is the Service Control Manager?

  1. svchost.exe
  2. services.exe
  3. lsass.exe
  4. wininit.exe

In what order must services be stopped?

  1. Alphabetical
  2. The same order they were started
  3. Reverse start order, so dependents stop before what they depend on
  4. Any order

Why does a modern Windows 11 PC show so many svchost.exe processes?

  1. Malware
  2. Each svchost hosts one (or a few) service DLLs, so a crash affects only that service
  3. svchost is a browser helper
  4. One svchost runs per signed-in user