1

how to stop/start a windows service in cmd with just a fragment of the service name. Example:

NET STOP *part_of_name_of_service*
NET START *part_of_name_of_service*

For example. mysql has many versions of "the name of service".

MySQL57
wampmysqld
etc, etc, etc

The idea is to stop/start any service that contains the word "mysql". Something like that:

net start *MySQL*

But unfortunately my attempt generates error: "The service name is not valid".

Attempts:

1.Dump service names to a file with the following command:

REG QUERY "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services" > services.txt
findstr /i /r "mysql" services.txt
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MySQL
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\wampmysqld64

but I could not complete the loop

2.With taskkill

call :winservices "*mysql*"

:: funcion winservices
@echo off
  goto:eof
  :winservices
  set winservices=%1
   taskkill /f /im "%winservices%" /t
  goto:eof

But "*mysql*" not works (only "mysql*" works)

The same happens when trying to delete the service:

sc delete *MySQL*

how I do this?

acgbox
  • 376
  • 1
  • 5
  • 21
  • I don't know with certainty that this will work, but you might try using a WQL (Windows Query Language) statement. – Davidw Mar 04 '20 at 22:34
  • 1
    A service `Name` could differ from its `DisplayName` and usually both differ from the related process name (if the service is running). Do not confuse them. Moreover, stopping a service differ from killing related process substantially. Although the `wmic.exe` utility is considered deprecated, you could parse output from `wmic path Win32_Service where "Name like '%%mysql%%' or DisplayName like '%%mysql%%'" get /Value` using the [`FOR /F` loop](https://ss64.com/nt/for_cmd.html)… – JosefZ Mar 07 '20 at 16:42

2 Answers2

1

Very simple with PowerShell:

Stop-Service *mysql*

Start-Service and Remove-Service work the same, the latter is only available from PowerShell 6 onwards.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
0

found a solution here (only for kill service)

taskkill /FI "IMAGENAME eq httpd*" /T /F
taskkill /FI "IMAGENAME eq mysql*" /T /F
acgbox
  • 376
  • 1
  • 5
  • 21