How to collect each service name and its status in Windows?

I want to get all server_name and its status without using a third-party tool. So far, the SC team has been good enough to get one of the values, something like

sc query | findstr SERVICE_NAME

but I also need STATUSfor everyone SERVICE_NAME.

+6
source share
4 answers

Here is the command that should complete the task:

for /f "tokens=2" %s in ('sc query state^= all ^| find "SERVICE_NAME"') do
    @(for /f "tokens=4" %t in ('sc query %s ^| find "STATE     "') do @echo %s is %t)

How it works:

The first is executed sc query state= all | find "SERVICE_NAME". This command is designed to provide you with service names, one per line. The carriages ^(which I deleted here) are needed to avoid special characters that you want to affect the command sc, not the command for.

for /f , SERVICE_NAME: , . :

C:\>for /f "tokens=2" %s in ('sc query state^= all ^| find "SERVICE_NAME"') do @echo %s
AdobeFlashPlayerUpdateSvc
AeLookupSvc
ALG
AppIDSvc
Appinfo
AppMgmt
aspnet_state
AudioEndpointBuilder
AudioSrv

for /f, sc query servicename, "" ( ).

, ( - , ).

:. , (, %s) .

+6

ss64.com/nt/sc.html

sc state= active¦inactive¦all
+1

SC.exe

sc.exe query state= all

(: sc query state=all . . : [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not exist as an installed service.)

: https://ss64.com/nt/sc.html

0

sc query | findstr SERVICE_NAME

only active (RUNNING) services will be displayed, so there is no need to filter the status (STATE).

Try SC with findstr syntax that supports multiple filtered elements:

sc query state= all | findstr "SERVICE_NAME STATE"

or

sc query state= all | findstr "DISPLAY_NAME STATE"

or both

sc query state= all | findstr "SERVICE_NAME DISPLAY_NAME STATE"
0
source

All Articles