Check if Service is running and start it when it is not running.

Check if Service is running and start it when it is not running.

Created a batch script that will check if a service is running and will start it when it is not running. It is also usable with a scheduled task. Plain and simple, see below:

@ECHO OFF
SET SvcName=ServiceName

SC QUERYEX "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
    ECHO %SvcName% is not running 
    ECHO START %SvcName%

    NET START "%SvcName%" > NUL || (
        ECHO "%SvcName%" wont start 
        EXIT /B 1
    )
    ECHO "%SvcName%" is started
    EXIT /B 0
) || (
    ECHO "%SvcName%" is running
    EXIT /B 0
)

You only need to change in the second line ServiceName to the name of the service you would like to check. For example, to check if Windows Update is running, you need to change it to wuauserv. The name can be found if you open the properties in Services, see the picture below.

Windows Update Properties

If you want to use it in a scheduled task and don’t want to create a lot of scripts, you can change the script to:

@ECHO OFF
SET SvcName=%~1

SC QUERYEX "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
    ECHO %SvcName% is not running 
    ECHO START %SvcName%

    NET START "%SvcName%" > NUL || (
        ECHO "%SvcName%" wont start 
        EXIT /B 1
    )
    ECHO "%SvcName%" is started
    EXIT /B 0
) || (
    ECHO "%SvcName%" is running
    EXIT /B 0
)

You can start the task with check-service.cmd “wuauserv” or any other service name. See the picture:

Branko Vucinec

About Branko Vucinec

Hi! I'm Branko, a Systems Engineer focused on Microsoft technologies from the Netherlands. I enjoy helping organizations with the business and people opportunities and challenges surrounding tech.

Comments