Skip to main content

How to Get a Service's Dependencies in Batch Script

Windows Services often operate in a complex web of relationships. Many services (like the Print Spooler or SQL Server) rely on other fundamental services (such as the RPC mapper or Network login) to function correctly. When troubleshooting why a service fails to start, knowing its dependencies is often the most important clue.

This guide will explain how to use the sc qc command in a Batch script to retrieve and parse the list of dependencies for any Windows Service.

The Tool: SC QC (Query Config)

While sc query tells you the status of a service, sc qc provides the configuration. This output includes the executable path, start type, and the "DEPENDENCIES" list.

Basic Retrieval Method

If you run this command on its own, it produces several lines of technical data:

sc qc "Spooler"

Partial Output:

[SC] GetServiceConfig 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

How to Extract Dependencies

To use this information in a script, we need to isolate the "DEPENDENCIES" lines. The PowerShell bridge provides the most reliable extraction since sc qc output can span multiple lines for dependencies.

The Extraction Script

@echo off
set "TargetSvc=Spooler"

:: Verify the service exists
sc query "%TargetSvc%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Service '%TargetSvc%' does not exist.
pause
exit /b 1
)

echo [QUERY] Retrieving dependencies for %TargetSvc%...
echo.

:: Use PowerShell for reliable multi-line dependency extraction
powershell -NoProfile -Command ^
"$svc = Get-Service -Name '%TargetSvc%' -ErrorAction SilentlyContinue;" ^
"if (-not $svc) { Write-Host '[ERROR] Service not found.'; exit 1 };" ^
"$deps = $svc.ServicesDependedOn;" ^
"if ($deps.Count -eq 0) { Write-Host '[INFO] Service has no dependencies.' }" ^
"else {" ^
" Write-Host 'Dependencies for %TargetSvc%:';" ^
" foreach ($d in $deps) { Write-Host (' - ' + $d.ServiceName + ' (' + $d.DisplayName + ')') }" ^
"}"

echo.
pause

Explaining the Logic:

  1. Get-Service: Queries the service by its internal name.
  2. ServicesDependedOn: Returns an array of all services this service depends on, handling multi-line dependencies automatically.
  3. Both ServiceName and DisplayName are shown so the operator can identify each dependency by its internal name (for scripting) and its friendly name (for understanding).

Alternative: Using SC QC Directly

If you prefer to stay within pure batch commands and need only the first dependency, you can parse the sc qc output:

@echo off
setlocal enabledelayedexpansion

set "svc=Spooler"

:: Verify the service exists
sc query "%svc%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Service '%svc%' does not exist.
pause
exit /b 1
)

echo [QUERY] Dependencies for %svc%:
echo.

:: Extract all dependency lines from sc qc output
set "FoundDeps=0"
for /f "tokens=1,* delims=:" %%a in ('sc qc "%svc%" 2^>nul') do (
set "label=%%a"
set "value=%%b"
if "!label: =!"=="DEPENDENCIES" (
if defined value (
set "dep=!value: =!"
echo - !dep!
set "FoundDeps=1"
)
)
)

if "!FoundDeps!"=="0" (
echo [INFO] No dependencies found.
)

echo.
pause
endlocal
info

Some system services listed in dependencies might appear in lowercase (e.g., rpcss). In Batch scripts, service commands like net start or sc are case-insensitive, so these lowercase names will still work perfectly.

Best Practices and Rules for Dependency Checking

1. Use Service Names (not Display Names)

Like all sc commands, you must use the internal Service Name (e.g., LanmanWorkstation) rather than the Display Name (e.g., Workstation). Using the Display Name will result in a "[SC] OpenService FAILED 1060" error.

2. Checking for Circular Dependencies

Before you manually add a dependency using sc config, always use sc qc to see the current state. Accidentally creating a circular dependency (Service A needs B, and B needs A) will cause both services to fail to start during boot.

3. Administrator Privileges

While querying configuration is generally permissive, on locked-down servers or for security-related services, you should run your command prompt as an Administrator to ensure sc qc has the necessary read permissions.

How to Avoid Common Errors

Wrong Way: Parsing only the first DEPENDENCIES line

Some system services have many dependencies that span multiple lines in the sc qc output. Each continuation line has the same : value format but without the DEPENDENCIES label.

Correct Way: Use the PowerShell approach (Method 1), which handles multi-dependency services automatically through the ServicesDependedOn property. If using pure batch, be aware that the for /f approach captures only dependencies that appear on the labeled line.

Best Practice: Verifying Each Dependency's Status

If you find dependencies, the next step is usually to check whether each one is running.

@echo off
set "TargetSvc=Spooler"

echo [CHECK] Verifying dependencies for %TargetSvc%...

powershell -NoProfile -Command ^
"$svc = Get-Service -Name '%TargetSvc%' -ErrorAction SilentlyContinue;" ^
"if (-not $svc) { Write-Host '[ERROR] Service not found.'; exit 1 };" ^
"foreach ($d in $svc.ServicesDependedOn) {" ^
" $status = $d.Status;" ^
" if ($status -ne 'Running') {" ^
" Write-Host ('[WARNING] ' + $d.ServiceName + ' is ' + $status)" ^
" } else {" ^
" Write-Host ('[OK] ' + $d.ServiceName + ' is Running')" ^
" }" ^
"}"

pause

Conclusions

Knowing what a service depends on is a vital part of root-cause analysis in Windows. By using Get-Service through a PowerShell bridge or parsing sc qc output, you can build powerful diagnostic tools that map out service relationships and explain why a "healthy" service refuses to start. Always target the internal service name for the most accurate results.