24

Currently, we have setup a .bat file which lists all services to start / stop them eg.

SC start SERVICE1
SC start SERVICE2

SC stop SERVICE1
SC stop SERVICE2

We add new services all the time and the list grows and is difficult to maintain in the batch file.

Is it possible to use a WILDCARD like 'SC start SERVICE*' or something?

Wolf
  • 312
  • 4
  • 18
Edward Tung
  • 241
  • 1
  • 2
  • 3

3 Answers3

21

Easy, via Powershell:

Get-service SERVICE* | stop-service -force

Get-service SERVICE* | start-service
Excellll
  • 12,627
  • 11
  • 51
  • 78
Gotxi
  • 209
  • 2
  • 2
  • 1
    I upvoted this one because it runs WAY faster than wmic. On my machine it takes about 2 milliseconds to get a list of services this way. It takes about 13000 milliseconds using wmic. – arjabbar Dec 01 '16 at 14:23
  • how do i combine these two commands in single go? – Raja Anbazhagan May 17 '17 at 15:03
  • @RajaAnbazhagan What do you mean by that? Restarting multiple services? I mean they are already combined here. – Wolf Feb 26 '21 at 12:02
  • Nevermind. I was hoping to not use the pipe also this solution was using two commands (one for stop and another for start). I found that the `Restart-Service` does them both "in single go". Checkout my answer. – Raja Anbazhagan Feb 28 '21 at 07:08
  • Not sure if I should start a new topic for that, but how do I filter out the services that are disabled before actually trying to start them https://superuser.com/q/1742116/1018595 ? – Sybuser Sep 13 '22 at 09:20
20

You can use wmic and SQL-ish wildcard syntax.

From a cmd console:

wmic service where "name like 'SERVICE%'" call startservice

From a .bat script:

wmic service where "name like 'SERVICE%%'" call startservice

Available verbs include startservice, stopservice, pauseservice, resumeservice, and others. Do wmic service call /? for more info.

rojo
  • 623
  • 3
  • 9
  • 1
    The query language is called WQL, BTW. It's a subset of SQL. – Bob Apr 25 '13 at 17:58
  • @Bob - Oh. Ya learn something new every day. `:>` – rojo Apr 25 '13 at 17:59
  • 1
    Just wanted to add one more thing. I ran into an error where the service name was not recognized. Turns out a service has a Service Name and a Display Name. The Service Name should be used, not the Display Name. You can find the Service Name with `sc query` – jdramer May 20 '15 at 14:47
2

if you want a One Line command,

You can use Restart-Service Cmdlet which is pre built in powershell.

To use Restart-Service simply call the cmdlet followed by the service name:

Restart-Service mysql57

To restart multiple services just specify the name of each service, separated by commas:

Restart-Service mysql57,apache

If you prefer, add the -displayname parameter and specify the service display name (the name shown in the Services snap-in) instead:

Restart-Service -displayname "Mysql 5.7 server"

This Cmdlet accepts wildcard matching as well. To restart all services starting with "mysql":

Restart-Service mysql*