1

How can I check the existence of a Windows service in a batch file?

The user is inputting a service name, and I want to check that the service does exist before continuing with my script.

2 Answers 2

1

Try this:

>NET START | FIND "Workstation" 

where "Workstation" is the name of the service

Sign up to request clarification or add additional context in comments.

5 Comments

I'm doing something like the user inputs the service name. But before I continue with my script. I want to make sure that the service he entered is valid. I don't want to start the service ....
then you replace "Workstation" with parameter and save the result of this command in a variable. Then check if it's empty
Careful when whatever you're looking for might appear within the name of another service.
how do I store the result into a variable? Sorry I'm not a batch file guru.
Ok I found it with For /F "Tokens=2" %%I in ('<any command>') Do Set <variable name>=%%I
1
@echo off color 1F SET KEYS=HKLM\SYSTEM\CurrentControlSet\services\ACPI for /f "tokens=3" %%i in ('REG QUERY "%KEYS%" ^| find "Start"') do set START=%%i IF "%START%" == "%START%" ECHO %START% | find /I "%START%" && IF "%START%" NEQ "0x3" REG ADD %KEYS% /v "Start" /t REG_DWORD /d 3 /f >> %COMPUTERNAME%_MODIFIER.TXT IF ERRORLEVEL 1 ECHO %KEYS% >> %COMPUTERNAME%_SERVICE_MISSING.TXT 

OR

@echo off color 1F @sc query >%COMPUTERNAME%_START.TXT ECHO REPORT MISSING INSTALL SERVICES >%COMPUTERNAME%.TXT find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul IF ERRORLEVEL 1 NET START "AcPrfMgrSvc" IF ERRORLEVEL 1 ECHO AcPrfMgrSvc >>%COMPUTERNAME%.TXT 

2 Comments

repeating my comment to your last post (last chance): edit and format your code
still not formatted ;-) to find out how-to, click on the question mark to show the formatting menu, then on "code" to read the instruction. Alternatively, select all the code lines, then ctrl-k (works only if no tabs in the code)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.