Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to stop and start a Windows service from a remote PC using the Windows command line, in a batch file.

sc \\ stop <ServiceName>

rem sc \\ query <ServiceName> | findstr STATUS | SET VAR=

However, I want to wait until the status of the service is assured to be stopped or started. So I was planning to loop and check the status continuously until the status is STOPPED.

I think that repeated request to the server may be processor intensive but there is no wait for the command line except for some hacks on checking time. But my main question is how do I get the status of the Windows service so I can check with an IF statement if it is OK to proceed to the next command?

share|improve this question
up vote 4 down vote accepted

To set a variable to the output of a command, use for /f:

for /f "tokens=*" %%a in ('command') do set _CmdResult=%%a

The problem is, to use a pipe in the command you need to escape it with the command line escape character: ^, therefore: ^|.

for /f "tokens=*" %%a in ('sc \\ query <ServiceName> ^| findstr STATUS') do set _CmdResult=%%a

Now, I'm not sure which version of Windows you're running, but my attempts at a sc query on Windows 7 give the following output:

>sc query SamSs
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

This means a findstr STATE would be required, which gives the output:

>sc query SamSs | findstr STATE
        STATE              : 4  RUNNING

Now, in the example above, tokens=* means every token is set to the variable %%a, where tokens are separated by spaces. In this case, you can use either the third token (4) or fourth token (RUNNING). By the way, with testing, 1 corresponds to STOPPED for me.

To make things easier, I'll be using the string RUNNING in this example. So we want the fourth token.

for /f "tokens=4" %%a in ('sc \\ query <ServiceName> ^| findstr STATE') do set _CmdResult=%%a

Of course, depending on what your sc query outputs, yours may be slightly different, so follow how I got there and modify as needed. Or post in a comment the output of your sc query and I'll modify as needed.

To check the value, you can use:

if %_CmdResult%==STOPPED (
    REM do something here

In your case, the loop would go something like this.

for /f "tokens=*" %%a in ('sc \\ query <ServiceName> ^| findstr STATUS') do set _CmdResult=%%a
if %_CmdResult%==STOPPED (
    sc \\ start <ServiceName>
    goto endloop
timeout /t 5
goto loop

Note that timeout is a utility only included in Windows Vista and later. Windows XP can use something like ping -n 1 -w 5000 > nul.

share|improve this answer
Thanks Bob, It was very detailed. But I guess the tiny bit that was missing in my code was the escaped pipe using the caret character. But this would be helpful for future reference. :) – Nassign Mar 28 '12 at 9:29

A More advanced example with higher complexity level

(but also higher usability)

  • using several common application (cURL, GREP, SED) you can either download a Win32 pre-build versions of those or use cygwin's exe files in Windows- both works perfectly fine (for cygwin, better just put c:\cygwin\bin\ folder location in your system's PATH variable for easier access).
  • PIPE processing (STDOUT and STDERR are processed through).
  • escaped symbols (|, >, &) - best practice / required on Windows7+.
  • no loops, no goto
  • working with numeric data (curl's response header Content-Length)

@echo off

setlocal enableextensions
  for /f "tokens=*" %%a in ('curl --head --ipv4 --sslv3 --silent --location-trusted --url "" 2^>^&1 ^| grep "Content-Length" 2^>^&1 ^| sed "s/\r\n$//g" 2^>^&1 ^| sed "s/content-length\:\ //gi" 2^>^&1') do ( set /a num=%%a )

  set /a num=%num% / 1024 / 1024

  echo it is %num%MB

will output it is 41MB

(original header is Content-Length: 43597312)

took it from here: - CMD/Bash Script Ninja - cURL Response Header Number Manipulation

share|improve this answer

You may want to check PsTools by Microsoft (Sysinternals). There is an tool called PsService where you can remotely check the services via the parameter \\computer

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .