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?
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.
In order to stop the service via batch script: Open cmd as administrator. Run this command: NET STOP <SERVICE_NAME>
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With