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.
Here’s a command that should do the job:
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 thesccommand and not theforcommand itself.Then the initial
for /fparses 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:This output is then fed to the next
for /f, which runssc 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.