Batch file to start or stop a service depending on its current state


This batch file will start or stop a service depending on its current state.
Just replace yourservice on line 5 and 17 with the name of the service you want  to stop or start.

@ECHO OFF

REM Checking Service state
REM replace yourservice below with the name of the service
net start yourservice 2>nul
if errorlevel 2 goto AlreadyRunning
if errorlevel 1 goto Error

REM Service started

GOTO ContinueWithBatch

:AlreadyRunning
REM Service is already running
REM replace yourservice below with the name of the service
net stop yourservice
GOTO ContinueWithBatch

:Error
REM Service failed to start
GOTO ContinueWithBatch

:ContinueWithBatch
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: