How to Query a Service's Configuration Details in Batch Script
Beyond knowing if a service is running, you often need to know how it is configured. Is it launching the correct executable? Is it set to start automatically or only when requested? Does it run as a user account or as the system? This underlying configuration determines the reliability and security of the service.
This guide will explain how to use the sc qc command to retrieve detailed technical parameters for any Windows service using a Batch script.
The Tool: SC QC (Query Configuration)
While sc query provides the current status, the sc qc command queries the Service Control Manager database for the static configuration settings.
Basic Retrieval Method
To see the configuration for a specific service (e.g., "Spooler"), run this command:
sc qc "Spooler"
Common Output Fields:
TYPE: Whether it's a shared process or a standalone app.START_TYPE: 2 (Auto), 3 (Manual), 4 (Disabled).BINARY_PATH_NAME: The exact command line and file used to launch the service.SERVICE_START_NAME: The account identity (e.g.,LocalSystem).
Extracting Specific Values with Batch
When writing an automation script, you don't want to read a wall of text. You usually want to extract a specific value, like the executable path, into a variable.
Script: Extract the Binary Path
This is useful for verifying that a service is pointing to the correct version of a file.
@echo off
setlocal enabledelayedexpansion
set "svc=Spooler"
:: Verify the service exists
sc qc "%svc%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Service '%svc%' is not installed.
pause
exit /b 1
)
echo [QUERY] Checking configuration for '%svc%'...
:: Use FOR /F to find the line containing BINARY_PATH_NAME
set "binPath="
for /f "tokens=1,* delims=:" %%a in ('sc qc "%svc%" ^| findstr /c:"BINARY_PATH_NAME"') do (
set "binPath=%%b"
)
:: Clean up the leading space from the result
if defined binPath set "binPath=!binPath:~1!"
if defined binPath (
echo [INFO] Executable location: !binPath!
) else (
echo [WARNING] Could not extract the binary path.
)
pause
endlocal
Extracting the Startup Account
Identifying which account a service runs under is essential for security audits.
@echo off
setlocal enabledelayedexpansion
set "svc=Spooler"
:: Verify the service exists
sc qc "%svc%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Service '%svc%' is not installed.
pause
exit /b 1
)
echo [QUERY] Checking service identity for '%svc%'...
echo.
:: Extract multiple configuration fields
set "startType="
set "startName="
set "binPath="
for /f "tokens=1,* delims=:" %%a in ('sc qc "%svc%"') do (
set "label=%%a"
set "value=%%b"
if "!label: =!"=="BINARY_PATH_NAME" if defined value set "binPath=!value:~1!"
if "!label: =!"=="START_TYPE" if defined value set "startType=!value:~1!"
if "!label: =!"=="SERVICE_START_NAME" if defined value set "startName=!value:~1!"
)
echo Binary Path: !binPath!
echo Start Type: !startType!
echo Runs As: !startName!
pause
endlocal
Comparing Configurations with WMIC
If you need a quick summary of attributes or want to check multiple services at once, wmic provides a single-line alternative.
@echo off
set "svc=Spooler"
echo [QUERY] Service configuration summary for '%svc%':
echo.
wmic service where "Name='%svc%'" get Name, StartMode, PathName, StartName 2>nul
if %errorlevel% neq 0 (
echo [ERROR] Service '%svc%' not found or query failed.
)
echo.
pause
How to Avoid Common Errors
Wrong Way: Using the Display Name
If you try sc qc "Print Spooler", it will fail unless the internal service name happens to be the same as the display name.
Correct Way: Always use the Service Name (e.g., Spooler). You can find this in the properties of any service in services.msc.
Problem: Quotes in the Binary Path
When extracting the BINARY_PATH_NAME, you will often get results like "C:\Program Files\App.exe" -service. If you try to use this path in another command, ensure your script handles the embedded quotes correctly to avoid a "file not found" error.
Best Practice: Error Handling
If the service does not exist, sc qc will return a non-zero exit code. You should always check %errorlevel% before trying to parse the output.
sc qc "%svc%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Service '%svc%' is not installed.
exit /b 1
)
Best Practices and Rules
1. Administrative Privileges
Querying a service's configuration metadata is generally allowed for standard users, but to view information for certain protected services or to get the full ServiceStartName of a third-party app, you should run your Batch script as an Administrator.
2. Identifying "Hidden" Command Line Flags
The BINARY_PATH_NAME is the only way to see if a service is running with special flags (like --config=C:\data.ini). Check this field if a service starts but behaves incorrectly.
3. Check for ERROR_CONTROL
The ERROR_CONTROL field tells you what Windows will do if the service fails during boot.
1 (NORMAL): Log the error and continue.2 (SEVERE): Reboot into Last Known Good configuration.
Conclusions
Querying a service's configuration via Batch script is a critical part of professional system auditing and verification. By mastering the sc qc command and using FOR /F loops to isolate specific fields like the binary path or startup account, you can build powerful diagnostic tools that explain why a system is behaving a certain way. This level of technical insight is essential for maintaining a secure and reliable Windows environment.