I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:
sc query \"ServiceName\"
I noticed no one mentioned the use of regular expressions when using find
/findstr
-based Answers. That can be problematic for similarly named services.
Lets say you have two services, CDPUserSvc
and CDPUserSvc_54530
If you use most of the find
/findstr
-based Answers here so far, you'll get false-positives for CDPUserSvc
queries when only CDPUserSvc_54530
is running.
The /r
and /c
switches for findstr
can help us handle that use-case, as well as the special character that indicates the end of the line, $
This query will only verify the running of the CDPUserSvc
service and ignore CDPUserSvc_54530
sc query|findstr /r /c:"CDPUserSvc$"