0

Is there a cleaner way to retrieve and assign the service name to a variable in cmd ?

My current attempt:

set svc_query="sc queryex type= service state= all | find /i "plusfield" |  find /i "service""
for /F "tokens=*" %%i in (' %svc_query% ') do set x=%%i

Which gets me x = SERVICE_NAME: PlusField01 so i remove the SERVICE_NAME: part

set svc=%x:~14%
echo %svc%
PlusField01

It works but as as a newb on cmd it seems that something its not right and maybe theres a cleaner way of doing it

  • Is there any reason you cannot use Powershell instead? – jscott Jun 13 '17 at 19:11
  • I´m making a change on a batch script already in use. For pshell i´d have to make a new script and learn power shell syntax which would take longer – user3682983 Jun 13 '17 at 20:18
  • My point was that cmd is much less flexible than powershell for this kind of work. You might be better of just querying the registry, rather than parsing `sc` output. – jscott Jun 13 '17 at 20:20
  • Thanks , i searched for ["sc query example"](https://stackoverflow.com/questions/12172997/how-to-collect-each-service-name-and-its-status-in-windows) and i found a slighly better solution: `for /f "tokens=2" %s in ('sc query state^= all ^| find /i "plusfield"') do @echo %s` – user3682983 Jun 13 '17 at 20:34

0 Answers0