Skip to main content

How to Query the Status of a Service in Batch Script

In system administration and automation, scripts often depend on the state of a Windows service. You might need to check if a database service is running before launching an application, verify that a backup service has stopped before updating its files, or simply monitor the health of a critical service. The standard, built-in command-line utility for all service management is SC.EXE (Service Control).

This guide will teach you how to use the sc query command to get the current status of any service. You will learn how to parse its output to capture the state (e.g., "RUNNING", "STOPPED") into a variable, a crucial skill for building intelligent, state-aware scripts.

The Core Command: SC QUERY

The sc.exe utility's query command is used to display information about a service, with its current state being the most important piece of data.

Syntax: SC QUERY <ServiceName>

  • QUERY: The sub-command to get information.
  • <ServiceName>: The short, internal name of the service (e.g., Spooler), not its long, friendly Display Name.

Understanding the Service STATE

The sc query command returns a lot of information, but the most important line for scripting is the STATE. This tells you what the service is currently doing.

StateDescription
RUNNINGThe service is currently running.
STOPPEDThe service is not running.
START_PENDINGThe service is in the process of starting.
STOP_PENDINGThe service is in the process of stopping.

Basic Example: Displaying a Service's Status

Let's query the status of the "Print Spooler" service, whose short name is Spooler.

@ECHO OFF
ECHO --- Querying Status for 'Spooler' service ---
ECHO.
SC QUERY Spooler

The output is a detailed block of information. The STATE line is what we are most interested in.

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

How to Capture the Status in a Variable

To use the service's state in your script's logic, you need to parse the verbose output and extract just the state word (e.g., "RUNNING"). The best way to do this is to pipe the output to find or findstr to isolate the correct line, and then use a FOR /F loop to grab the desired word.

@ECHO OFF
SET "ServiceName=Spooler"
SET "ServiceStatus="

ECHO --- Capturing Status for '%ServiceName%' ---

REM 'tokens=4' grabs the 4th "word" from the line containing "STATE".
FOR /F "tokens=4" %%S IN ('SC QUERY "%ServiceName%" ^| find "STATE"') DO (
SET "ServiceStatus=%%S"
)

IF NOT DEFINED ServiceStatus (
ECHO [FAILURE] Could not find the service.
) ELSE (
ECHO [SUCCESS] The status is: %ServiceStatus%
)

How the Command SC QUERY Works

The SC QUERY command communicates directly with the Service Control Manager (SCM), a core Windows process that is responsible for starting, stopping, and managing all services. The SCM maintains the current state of every service registered on the system, and sc query provides a direct way to read this state information.

Common Pitfalls and How to Solve Them

Problem: Using the Wrong Service Name (Display vs. Service)

This is the most common error. The SC command requires the short, internal Service Name, not the friendly Display Name.

Display NameService Name
Windows Timew32time
Windows Updatewuauserv

Example of error message:

C:\> SC QUERY "Windows Update"
[SC] EnumServicesStatus:OpenService FAILED 1060:

The specified service does not exist as an installed service.

Solution: You must use the correct service name. If you only know the Display Name, you can find the Service Name with the SC GETKEYNAME command.

C:\> SC GETKEYNAME "Windows Update"
[SC] GetServiceKeyName SUCCESS
Name = wuauserv

Problem: The Output is Verbose and Hard to Parse

The default output is designed for humans. Without filtering, it's difficult to extract a single piece of information.

Solution: Always pipe the output to find or findstr. This is the standard pattern for isolating the specific line you need (STATE, BINARY_PATH_NAME, etc.) before you parse it with a FOR /F loop. This makes your script much cleaner and more reliable.

Practical Example: A "Prerequisite Check" Script

This script needs to run a task that depends on the SQL Server service being active. It checks the service's status and only proceeds if it's "RUNNING".

@ECHO OFF
SETLOCAL
REM The service name for the default SQL Server instance is MSSQLSERVER.
SET "RequiredService=MSSQLSERVER"
SET "ServiceState="

ECHO --- SQL Server Prerequisite Check ---
ECHO.
ECHO Checking status of the '%RequiredService%' service...

FOR /F "tokens=4" %%S IN ('SC QUERY "%RequiredService%" ^| find "STATE"') DO (
SET "ServiceState=%%S"
)

IF /I "%ServiceState%"=="RUNNING" (
ECHO [SUCCESS] The SQL Server service is running.
ECHO Proceeding with the database script...
REM (Your sqlcmd commands would go here)
) ELSE (
ECHO [FAILURE] The SQL Server service is not running (Current state: %ServiceState%).
ECHO Please start the service and run this script again.
)

ENDLOCAL

Conclusion

The sc query command is the definitive, built-in tool for checking the status of a Windows service from a batch script.

Key takeaways for using it effectively:

  • You must provide the correct internal Service Name, not the Display Name.
  • Pipe the output to find "STATE" to isolate the line containing the service's current state.
  • Use a FOR /F "tokens=4" loop to parse this line and capture the state (e.g., "RUNNING") into a variable.
  • Use this variable in an IF statement to build intelligent scripts that can react to the state of their dependencies.