Automate Service Management Through net stop and sc Command

Published:19 May 2021 - 4 min. read

Every Windows admin out there has to deal with Windows services at some point. Automating service management with command-line tools like net stop and the sc command comes in handy when scripting Windows services.

In this tutorial, you will learn how to manage Windows services in just about every way from the command prompt.

Let’s get started!

Prerequisites

If you’d like to follow along with this tutorial, please make sure you have the following:

  • A Windows 7+ computer. The tutorial will be using a Windows 10 Build 2004 machine.

Finding Service Names

Since the net and sc commands are both command-line tools, it’s sometimes not the easiest to find the actual service names to work with. Both commands require you to provide a service name, not the service display name. You find the service name in the services.msc console as shown below

service name in the services.msc console
service name in the services.msc console

Managing Services with the SC Command

Let’s kick off this service management tutorial with the sc command. The sc command is a built-in command-line tool that’s been built into Windows for a long time. This command can handle everything you need to manage Windows services from the command prompt.

All commands in this section will also work on remote computers. To run sc against a remote computer, specify the computer name in the command e.g. sc \\REMOTENAME <some command>.

1. To get started, open a command prompt as administrator.

2. Run the sc query command. The sc query command queries all Windows services and returns information such as the name (SERVICE_NAME), display name (DISPLAY_NAME), state, and more. If you don’t know the service name, sc query is your friend to find it.

Run the sc query command
Run the sc query command

3. Once you have the service name you’d like to work with, append the name to sc query like below to limit the output only to that particular service. You can see below that the Windows Update Agent service is STOPPED.

sc query wuauserv
Querying wuauserv service
Querying wuauserv service

4. Next, start the service by invoking sc start followed by the name of the service. Once you do, you’ll see that Windows puts the service into a START_PENDING state while it’s starting. sc start wuauserv

sc start wuauserv
Starting wuauserv service
Starting wuauserv service

5. Wait a few seconds and check the status of the service with sc query. You should now see that the service is running.

Querying wuauserv service status
Querying wuauserv service status

6. Along the same line as starting a service, you can also stop a service with the sc command using the sc stop command. You’ll see the service turn into a STOP_PENDING state. After a few seconds, check the status with sc query like before.

sc stop wuauserv
 sc query wuauserv
Stopping wuauserv service
Stopping wuauserv service

7. Perhaps you’d prefer to suspend the service rather than stopping it. In that case, simply use the sc pause command. You’ll see the service immediately goes into a PAUSED state. The below example is using the apphostcsv service as an example.

All the services do not support pausing. You can find all services that support pausing by running sc query and ensuring the service has a PAUSABLE state.

sc pause apphostsvc
Pausing service using SC pause
Pausing service using SC pause

You’d then see in the Windows services applet that the service is paused.

Verifying AppHostSvc service pause
Verifying AppHostSvc service pause

When a service is running, it maintains its internal state and cache information. When stopped, the service clears all the cache and its internal state. When paused, the service maintains the cache and internal state.

8. To resume a suspending/paused service, run the sc continuecommand. You’ll then see the service immediately goes back to a RUNNING state. sc continue apphostsvc

sc continue apphostsvc
Continuing apphostsvc service
Continuing apphostsvc service

Managing Services with the NET Stop Command

One of the oldest and most useful Windows admin commands is the net command. The net command can do a lot but one of its strengths is working with services. Using the net command, you can stop, start, pause and resume services with a single command like sc.

This section will be working with the BITS and Windows Update services as an example.

On a Windows PC:

1. Open a Command Prompt as administrator.

2. Use the net stop command to stop the BITS service providing the service name to stop. When you stop a service with net stop, you’ll see the service go into a pending state and then, if all goes well, a stopped state.

net stop BITS

Unlike the sc command, net can’t query the status of services.

Stopping BITS service
Stopping BITS service

3. To start the service back up, replace stop with start and net will perform the opposite effect. net start BITS

net start BITS 
Starting BITS service
Starting BITS service

If the service is already started, you will see a message stating “The requested service has already been started”. Similarly, if the service is stopped and you attempt to stop it, you will see “The service is not started” message in output.

4. To pause a running service, similar to sc, execute the net pause command as shown below. To continue, use the continue command.

net pause AppHostSvc
net continue AppHostSvc
Pausing AppHostSvc service
Pausing AppHostSvc service
Continuing AppHostSvc service
Continuing AppHostSvc service

Managing Multiple Services at Once

In the previous sections, you were working with a single service. Using the command prompt’s built in for construct, you can extend that functionality to work with multiple services at once.

To manage multiple services at once:

First, create a text file like the one below with each service you’d like to start or stop separated with a new line. The for construct will read this text file. This file will be saved in C:\Temp.

bits
wuauserv

Run the below command. This command consists of six components:

  • For for construct declaration
  • The /F switch to tell for to read a file
  • The tokens string to tell for which items in each line of the text file to process. In this case, the example uses * to tell for to read the entire line.
  • The in operator and file path to specify which file to read
  • The do construct to tell the for construct a command to execute
  • A placeholder for each line in the text file (%A) which represents each line in the text file as the for construct processes it

In the example below, the command prompt is reading the C:\Temp\Services.txt file with line-delimited service names and running service commands against each service in the file.

for /F "tokens=*" %A in (c:\Temp\Services.txt) do sc start "%A"
for /F "tokens=*" %A in (c:\Temp\Services.txt) do net start "%A"
for /F "tokens=*" %A in (c:\Temp\Services.txt) do sc query "%A"
Starting multiple services
Starting multiple services

Conclusion

In this tutorial, you have learned how to use the net and sc Windows commands to manage Windows services. You should now be able to stop, start, pause and resume Windows services via the command prompt.

How about taking the next step and learning how to manage Windows services with PowerShell?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!