If you try to find a little script that uses the SC command, which seems to have some limitations (and I could not test it):
@echo off setlocal enabledelayedexpansion :: Change this to your service name set service=MyServiceName :: Get state of service ("RUNNING"?) for /f "tokens=1,3 delims=: " %%a in ('sc query %service%') do ( if "%%a"=="STATE" set state=%%b ) :: Get start type of service ("AUTO_START" or "DEMAND_START") for /f "tokens=1,3 delims=: " %%a in ('sc qc %service%') do ( if "%%a"=="START_TYPE" set start=%%b ) :: If running: stop, disable and print message if "%state%"=="RUNNING" ( sc stop %service% sc config %service% start= disabled echo Service "%service%" was stopped and disabled. exit /b ) :: If not running and start-type is manual, print message if "%start%"=="DEMAND_START" ( echo Start type of service %service% is manual. exit /b ) :: If start=="" assume Service was not found, ergo is disabled(?) if "%state%"=="" ( echo Service "%service%" could not be found, it might be disabled. exit /b )
I do not know if this gives the behavior you wanted. SC does not seem to list the services that are disabled. But since you do not want to do anything if it is disabled, my code simply prints a message if the service is not found.
However, you can, hopefully, use my code as a framework / toolkit for your purposes.
EDIT:
Given npocmaka's answer, you can probably change the for sections to something like:
sc query %service%| find "RUNNING" >nul 2>&1 && set running=true
source share