The most direct way to query a Windows service status is using the sc query
command. Here's the precise syntax:
sc query "IISADMIN" | find "STATE"
This returns output like:
STATE : 4 RUNNING
Service states include:
- 1: STOPPED
- 2: START_PENDING
- 3: STOP_PENDING
- 4: RUNNING
- 5: CONTINUE_PENDING
- 6: PAUSE_PENDING
- 7: PAUSED
For a cleaner output format, use this PowerShell one-liner:
Get-Service "IISADMIN" | Select-Object Name,Status
Output:
Name Status
---- ------
IISADMIN Running
Here's a reusable batch script to check service status:
@echo off
setlocal
set service_name=IISADMIN
for /f "tokens=3" %%A in ('sc query "%service_name%" ^| find "STATE"') do (
if "%%A"=="RUNNING" (
echo Service %service_name% is running
) else (
echo Service %service_name% is not running (Status: %%A)
)
)
endlocal
To check services on remote computers using PowerShell:
Get-Service -Name "IISADMIN" -ComputerName "SERVER01" |
Format-Table MachineName,Name,Status -AutoSize
For legacy systems without PowerShell:
wmic service where "name='IISADMIN'" get name,state
Returns:
Name State
IISADMIN Running
Here's a complete list of service state codes from Microsoft documentation:
Code | Status |
---|---|
1 | STOPPED |
2 | START_PENDING |
3 | STOP_PENDING |
4 | RUNNING |
5 | CONTINUE_PENDING |
6 | PAUSE_PENDING |
7 | PAUSED |
When managing Windows services through the command line, you typically need more than just a binary "running/not running" status. Here are the most effective ways to get detailed service information:
sc query "IISADMIN" | findstr "STATE"
This will return output like:
STATE : 4 RUNNING
The numerical state codes from SC query mean:
- 1: STOPPED
- 2: START_PENDING
- 3: STOP_PENDING
- 4: RUNNING
- 5: CONTINUE_PENDING
- 6: PAUSE_PENDING
- 7: PAUSED
For more readable output, PowerShell provides better formatting:
Get-Service "IISADMIN" | Select-Object Name,DisplayName,Status
Sample output:
Name DisplayName Status
---- ----------- ------
IISADMIN IIS Admin Service Running
For frequent checks, create a service_status.bat:
@echo off
setlocal enabledelayedexpansion
set SERVICE_NAME=%1
if "%SERVICE_NAME%"=="" (
echo Usage: %0 service_name
exit /b 1
)
for /f "tokens=3" %%a in ('sc query "%SERVICE_NAME%" ^| findstr "STATE"') do (
set STATE=%%a
)
if "%STATE%"=="1" (set STATUS=STOPPED)
if "%STATE%"=="4" (set STATUS=RUNNING)
if "%STATE%"=="7" (set STATUS=PAUSED)
echo Service %SERVICE_NAME% is %STATUS%
For more complex scenarios where you need to check multiple services:
@echo off
for %%S in ("IISADMIN","W3SVC","MSMQ") do (
for /f "tokens=3" %%A in ('sc query %%S ^| findstr "STATE"') do (
echo %%S is in state: %%A
)
)
Both methods work for remote machines too:
sc \\server01 query "IISADMIN" | findstr "STATE"
Or with PowerShell:
Get-Service -ComputerName server01 -Name "IISADMIN"