Server Fault is a question and answer site for system and network administrators. 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

Firstly, please excuse my ignorance as I am completely new to scripting.. Here is what I am planning to do

  1. Stop the service

    (it would be good if i could check if service exists in the first place, otherwise, it should still continue on with the script correct?)

  2. Check if Win2k or WinXP, Uninstall the service and delete files according to OS type

  3. Install new application

  4. Start service

Here is what I have at the moment. Can someone please review and suggest ways of improvement? Many thanks in advance.

@echo off  
net stop "service name"  

Here, I want to check if the service exists, if yes, just go ahead and stop it, else, echo "Service not installed" and go ahead to uninstall_and_install routine, can i do as below?

if ERRORLEVEL 0 GOTO uninstall_and_install IF NOT echo Service not found GOTO uninstall_and_install  
IF NOT EXIST c:\Windows GOTO Win2k  
cd Windows\system32\  
"insert commands to uninstall and install 

cd Winnt\system32\  
"insert commands to uninstall and install 

share|improve this question

You might take some hints from the example below.

@echo off
set SERVICENAME=w32time

REM Check if the service is installed
REM ==================================================
    sc query %SERVICENAME% > nul 2>nul
    if %ERRORLEVEL% == 1060 goto serviceDoesNotExist

    net stop %SERVICENAME%

    REM Get Operating System Version
    for /F "usebackq delims==" %%i IN (`ver`) DO (set OSVersion=%%i)

    echo %OSVersion% | find "Microsoft Windows [Version 6.1.7600]"
    if %ERRORLEVEL% NEQ 0 GOTO :windows7

    echo Install the Application the Windows 7 way
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.