Wildcard Services restart

21

5

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

SC start SERVICE
SC start SERVICE

SC stop SERVICE
SC stop SERVICE

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

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

Edward Tung

Posted 2013-04-23T19:24:17.730

Reputation: 211

1What windows version? – Endoro – 2013-04-24T15:22:10.307

Answers

17

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

Posted 2013-04-23T19:24:17.730

Reputation: 523

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 – 2015-05-20T14:47:38.617

1The query language is called WQL, BTW. It's a subset of SQL. – Bob – 2013-04-25T17:58:14.367

@Bob - Oh. Ya learn something new every day. :> – rojo – 2013-04-25T17:59:14.937

13

Easy, via Powershell:

Get-service SERVICE* | stop-service -force

Get-service SERVICE* | start-service

Gotxi

Posted 2013-04-23T19:24:17.730

Reputation: 129

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 – 2016-12-01T14:23:30.240

how do i combine these two commands in single go? – Raja Anbazhagan – 2017-05-17T15:03:08.027

0

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*

Raja Anbazhagan

Posted 2013-04-23T19:24:17.730

Reputation: 101