Reputation: 1396
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?
Upvotes: 26
Views: 52577
Reputation: 61
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.
Upvotes: 0
Reputation: 51
what about:
sc interrogate "nameofyourservicehere"
I've found this really useful since tasklist
won't give informations on whether the service is installed or not. (or i did not find how)
Upvotes: 5
Reputation: 141
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.
Upvotes: 14
Reputation: 119816
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.
Upvotes: 68
Reputation: 33880
you can run "net stop [servicename]" if it fails with "the service name is invalid" the service isn't installed
Upvotes: -12