How To Control Running Services With The SC Command [Windows]
One of the things that I find myself doing much more often since diving into IT is monitoring and checking the status of services running in computers across the company.
There is the standard way of managing services which most people use because it’s well-known and fairly easy. This involves going into Administrative controls or going into the control panel, hunting down the Services utility and doing what you need to do.
If you want to manage the services that are running on a different computer or server on your network, you need to go to computer management, connect to the remote machine, and then manage those services.
There’s nothing at all wrong with taking this approach – in fact system administrators everywhere use these standard Windows utilities to manage the infrastructure across the enterprise. But, what if you could monitor, stop and start services by typing in a single line at the command prompt?
Controlling Services From the Command Line
You may need to stop services running in a client computer so that you can reinstall some software. Or maybe you need to restart services that are running on a server because some software isn’t working properly.
Up until now, you’ve probably managed services on client machines running on your network using the standard services utility. This is the Services tool that you launch in the Administrative Tools section of the control panel.
In Admin Tools, you can either go directly to the Services tool, or open up computer management if you want to access a different computer on your network.
To access a different computer, just click action, “Connect to Another Computer” and then type the network name of the computer.
Then you can open up the services tool.
An Easier Way To Manage Services
So anyway, that’s the long way. The shorter and faster way is to use the command “SC”.
You’re probably familiar with the NET command. The SC command is nearly identical to NET, except that it can also create a service, in addition to monitoring, stopping and starting them. Best of all, you can issue the SC command against other computers over the network (assuming you have admin rights).
Doing an SC query against a computer will give you all of the installed services and whether they are running. When you issue the command “SC \\**computer-name** query |more” – you’ll get an output as shown here.
As you can see, this gives you the state of the service, the type of service and other parameters. For the sake of our exercises here, we’re going to pay attention to just the state of the services.
If you want to list only the services that are currently not running (stopped), then you would issue the command, “SC \\**computer-name** query state= “inactive” |more”
The |more part of the command makes the output pause on each screen so that you can scroll.
As you can see, those services are all stopped. Now, instead of having to dig through all of those services, if you know the name of the service, you can just issue the command “SC \\**computer-name** query **service-name**”
That will report back with the current status of just that service and nothing else. To remotely stop a service, all you have to do is issue the stop command, “SC \\**computer-name** stop **service-name**”
The output will show a state of “STOP-PENDING”, but after a few moments, if you query that service again, you’ll see that it is no longer running. You can start it by issuing the same command, but replacing “stop” with “start”.
Creating A Script To Query, Stop Or Start Any Service
Okay, so now that you’ve got the basics down, it’s time to write a script that will stop any service that you want without the need to remember the exact command string. We can do this by writing a trusty Windows Script, as follows.
When you run this script, it’ll ask you for the name of the service that you want to stop or start.
And then whether you want to stop or start the service.
Using the “>> c:\temp\results.txt” command, it outputs the stop and start results and errors to a text file that you can use to see whether the command worked.
When I ran the script above, I followed it up with a quick query of the service before and after running the script.
As you can see, the service I stopped – helpsvc – was running. I ran the Windows Script to stop it, queried it, and it was stopped.
If you wanted to get really fancy, you could add a section to the script that outputted all of the currently installed services on the PC to a text file that you could review before deciding which service to stop or start. The command for that would be “sc \\” & strComputer & ” query > c:\temp\services.txt”.
As you can see, the SC command can be a pretty powerful and efficient way to manage services on any computer, and if you have some talent with Windows Scripting, you can even automate the process to make your job even easier.
Do you think the SC command could come in handy for you? What about an automated script that runs the command? Share your ideas and thoughts in the comments section below.
Image Credit: Shutterstock
Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.