Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one find out if a Windows service is installed using (preferably) only batch?

Tags:

I need to check if a Windows service is installed from a batch file. I can dip into something other than batch if I need to, but I would prefer not to. Is there any way to do this?

like image 280
kerkeslager Avatar asked Oct 07 '10 15:10

kerkeslager


People also ask

How do I know if a service is installed in CMD?

Here is an example using sc query to verify if a windows service is installed and stop if found. sc query | find /I "%tmpServiceName%" > nul if not errorlevel 1 echo. && net stop %tmpServiceName% if errorlevel 1 echo. - Windows service %tmpServiceName% is not running or doesn't exist.

How do I stop a Windows service from a batch file?

In order to stop the service via batch script: Open cmd as administrator. Run this command: NET STOP <SERVICE_NAME>

How do I run a service from a batch file?

The net start command is used to start the services. Also if the service has spaces in it's name, it must be enclosed in double quotes. Once you have entered the text, save and close the file. When you want to start up the program, all you need to do is double click on the batch file and it will run.


2 Answers

Try this:

@echo off SC QUERY ftpsvc > NUL IF ERRORLEVEL 1060 GOTO MISSING ECHO EXISTS GOTO END  :MISSING ECHO SERVICE MISSING  :END 

Note that the SC QUERY command queries by the short service name not the display name. You can find this name by looking at the General tab of a service's properties in Service Manager.

like image 198
Kev Avatar answered Sep 24 '22 15:09

Kev


You should using "query", not "Stop" or something else command, the "Stop" can stop your service if it's exist, then this is not the right way.

@echo OFF  set _ServiceName=SomeServiceName  sc query %_ServiceName% | find "does not exist" >nul if %ERRORLEVEL% EQU 0 echo Service Does Not Exist. if %ERRORLEVEL% EQU 1 echo Service Exist. 
like image 42
M Sasan MH Avatar answered Sep 23 '22 15:09

M Sasan MH