Start and stop multiple services with one command

Sometimes it’s really useful to start and stop a set of services at the same time, for example all Citrix services on a particular server. There are often as many as 15 or 20 of these services, so doing them one-at-a-time can be pretty time consuming.

Here’s one way of starting and stopping a set of services:

net start | findstr Citrix* > C:\services.txt
for /F "tokens=*" %A in (c:\services.txt) do net stop "%A"
for /F "tokens=*" %A in (c:\services.txt) do net start "%A"

What this does is uses the “net start” command to produce a list of running services. These are then piped with the | symbol to the “findstr” command, which searches through the data that is passed to it for the specified string, in this case “Citrix*”. The output of this is then saved to a text file called “C:\services.txt”.
We then use the “for” command to iterate through each line of this text file, and send it to the “net stop” command, to stop each service. We then repeat the process for “net start” to re-start them all.
This contains no error detection or correction, so isn’t suitable for unattended running in it’s current form, and it’s almost certainly possible to do this without the intermediate text file. It also doesn’t test for dependencies, so the services may not stop, or additional prompts may be generated.

I’m still tweaking this, so I’ll put an updated version up here when it’s done.

2 Comments

  1. Or you could do this>>

    net start | findstr %1 > .\Services.txt
    for /F “tokens=*” %%a in (Services.txt) do net stop “%%a” && net start “%%a”
    del .\Services.txt

    then if you saved this as restart.bat

    you could just run it like “restart citrix*”

    • Admin

      Yep, that’s a tidier and more flexible approach, thanks 🙂
      To be honest I think I’d just powershell this these days, will chuck a script for that up at some point.

Leave a Comment

*