AabinGunz
AabinGunz

Reputation: 12347

How to collect each Service name and its Status in Windows?

I want to fetch all service_name and its status without using any 3rd party tool. So far SC command was good enough to fetch one of the values, something like

sc query | findstr SERVICE_NAME

but I also need STATUS for each SERVICE_NAME listed.

Upvotes: 10

Views: 55298

Answers (5)

BaTTy.Koda
BaTTy.Koda

Reputation: 173

Thanks @jon for the post, that was very helpful. I wanted a column aligned output, so I grabbed your example above and created the following batch file:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO.Service                                                  State
ECHO.-------------------------------------------------------  -------------

FOR /F "TOKENS=2" %%S IN ('sc query state^= all ^| FIND "SERVICE_NAME" ^| FIND /I "myservice"') DO (
    @(FOR /f "TOKENS=4" %%T IN ('sc query %%S ^| FIND "STATE     "') DO (
        SET OUTPUT="%%S -------------------------------------------------------------"
        @ECHO !OUTPUT:~1,55!^> %%T
    ))
)
ENDLOCAL

That produces an output suitable for what I was looking for like this:

Service                                                  State
-------------------------------------------------------  -------------
MyService.Test_1 --------------------------------------> STOPPED
MyService.Test_2 --------------------------------------> RUNNING
MyService.Test_3 --------------------------------------> STOPPED

Here's a breakdown of the relevant changes

  • added the additional find to filter to only the service names I wanted to see instead of the whole list, in this case looking for service names that contain "myservice" FIND /I "myservice"
  • Since it's in a batch file, changed %s and %t to %%s and %%t respectively (I like to use caps for vars)
  • Added the OUTPUT var for formatting (SET OUTPUT="%%S -------------------------------------------------------------"), the value is quoted since I initially intended to use spaces, but switched to a dash -, that can be changed to any character you like, I considered * as well. The value is set to the service name plus whatever padding character.
  • The output line @ECHO !OUTPUT:~1,55!^> %%T
    • The formatted output: !OUTPUT:~1,55! uses ! because of the delayed expansion and takes 55 characters starting at the 2nd character (zero based index) in order to remove the leading "
    • ^>: the ^ escapes the > so it does not attempt to redirect to... somewhere. All in order to just include the > in the output line (totally not necessary)
    • Finally, %%T, the state output

Hope this helps someone! I can never remember this stuff!

Upvotes: 1

StackzOfZtuff
StackzOfZtuff

Reputation: 3108

Try Get-Service

PowerShell has Get-Service. It has a little less detail than sc.exe.

Try SC.exe

sc.exe query state= all

(Note: sc query state=all will NOT work. You NEED the space sign after the equals sign. Otherwise you will get this weird error: [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not exist as an installed service.)

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

Upvotes: 2

Petr Charousek
Petr Charousek

Reputation: 51

By the way

sc query | findstr SERVICE_NAME

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

Try SC with findstr syntax that supports multiple filtered items:

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"

Upvotes: 5

Jon
Jon

Reputation: 437844

Here's a command that should do the job:

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:

First sc query state= all | find "SERVICE_NAME" is run. This command is designed to give you the service names, one per line. The carets ^ (which I have removed here) are necessary in order to escape the special characters that you want to affect the sc command and not the for command itself.

Then the initial for /f parses the above output to remove the standard "SERVICE_NAME:" prefix from each line, giving you pure service names. At this point the output looks like this:

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

This output is then fed to the next for /f, which runs sc query servicename, finds the line with the state, and isolates the 4th "word" (the current state).

Finally, the name of each service is printed along with its state (at this point you can choose to do something different if you wish).

Important note: If you run this inside a batch file, the percent signs (e.g. at %s) need to be doubled.

Upvotes: 10

Nicholas Albion
Nicholas Albion

Reputation: 3284

ss64.com/nt/sc.html

sc state= active¦inactive¦all

Upvotes: 1

Related Questions