SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Microsoft Windows Tools, Applications, Tutorials, Tips and Tricks for Windows Users


SC Command Line Tool for Controlling, to Stop and Start Services on Windows

SC is one of the Windows XP Professional command-line tools.
SC or the executable file for this command line program sc.exe can be used to retrieve and set information about services running on the Windows computer.
SC command line tool communicates with the NT Service Controller and installed services on the computer.
It is possible to stop or start a service, and change its current status by using the SC.exe command line utility for XP users.

I used the SC.exe to stop or start a list of services by preparing a batch file which includes a series of SC.exe commands for stopping/starting the services I want.
You can also control the status of these services running on your computer by using the Services Microsoft Management Console (MMC) which is in the Administrative Tools menu.
You can set the Startup Type and Status properties in the Services Management Console.

But my requirement is when I concantrate on coding using the Microsoft Visual Studio IDE, and while building large solutions, I need all the sources that are consumed by not necessarily running services on my computer.
But when I'm not coding using the VS2008 IDE, I might use the MS SQL Server 2008 instance installed on my computer, so its service should be running by default, etc.
So I prepared a .bat file which includes all the SC STOP commands for services that I could stop, and run it by just clicking on the .bat batch file. So simple :)


SC Command Line Tool for stopping a service - hpqcxs08

In this section, I'll try to demonstrate how sc.exe can be used to stop a service with an example. I choose the "hpqcxs08" HP printer service as sample service. I'll use sc.exe command-line program to stop service with name "hpqcxs08".

Before running the command line tool SC.EXE you can find the short help guide about the sc.exe utility by just typing its "sc" and executing without parameters.

sc
Code

Here is the help guide for SC.exe command line utility for XP users.

DESCRIPTION:
 SC is a command line program used for communicating with the
 NT Service Controller and services.
USAGE:
 sc <server> [command] [service name] <option1> <option2>...

The option <server> has the form "\\ServerName"
Further help on commands can be obtained by typing: "sc [command]"
Commands:
query-----------Queries the status for a service, or
                        enumerates the status for types of services.
queryex---------Queries the extended status for a service, or
                        enumerates the status for types of services.
start-----------Starts a service.
pause-----------Sends a PAUSE control request to a service.
interrogate-----Sends an INTERROGATE control request to a service.
continue--------Sends a CONTINUE control request to a service.
stop------------Sends a STOP request to a service.
config----------Changes the configuration of a service (persistant).
description-----Changes the description of a service.
failure---------Changes the actions taken by a service upon failure.
qc--------------Queries the configuration information for a service.
qdescription----Queries the description for a service.
qfailure--------Queries the actions taken by a service upon failure.
delete----------Deletes a service (from the registry).
create----------Creates a service. (adds it to the registry).
control---------Sends a control to a service.
sdshow----------Displays a service's security descriptor.
sdset-----------Sets a service's security descriptor.
GetDisplayName--Gets the DisplayName for a service.
GetKeyName------Gets the ServiceKeyName for a service.
EnumDepend------Enumerates Service Dependencies.

The following commands don't require a service name:
sc <server> <command> <option>
boot------------(ok | bad) Indicates whether the last boot should
                        be saved as the last-known-good boot configuration
Lock------------Locks the Service Database
QueryLock-------Queries the LockStatus for the SCManager Database
EXAMPLE:
sc start MyService

Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:

Code

sc.exe command line tool

After have a quick look up in the help documentation for SC.EXE command, we can run the sc.exe with STOP command passing the name of the service "hpqcxs08"
Of course on of the main parameters of SC.Exe program is the server name that we want to control the services that are running on.

sc \\servername STOP "hpqcxs08"
Code

The server or computer name should be in the form \\Server-Name. And after the STOP command, you should pass the service name.

The sample output for the above SC.exe STOP service command is like:

SERVICE_NAME: hpqcxs08
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 3 STOP_PENDING
(STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0

Code


Preparing a Batch file (.bat) to Stop and Start a List of Services

In the first section of this tutorial, I said I use a batch file (.bat) to control a list of services, and this provides me a simplicity and gains me time.
Since I do not require to open the Services Management Console each time and find the services I want to stop among many others, and click STOP button, go to next service, etc.
Be sure a batch file is better.

Open an empty notepad document file.
Then write down the list of services with the SC.exe command parameters.
Be sure you use the service name as parameter.
And if you want to control a service that is running on an other computer that you have administrative priviledges then do not forget the server name or the computer name.

Save the text document as a batch file by changing its extension to .bat. Just double-click on the file to execute it.

Here is my sample .bat batch file.

sc \\servername STOP "hpqcxs08"
sc \\servername STOP "MSSQLFDLauncher$SQL2008RTM"
sc \\servername STOP "MSOLAP$SQL2008RTM"
sc \\servername STOP "ReportServer$SQL2008RTM"
sc \\servername STOP "MsDtsServer100"
sc \\servername STOP "SQLAgent$SQL2008RTM"
sc \\servername STOP "SQLBrowser"
sc \\servername STOP "wuauserv"
sc \\servername STOP "BITS"
sc \\servername STOP "helpsvc"
sc \\servername STOP "Irmon"
sc \\servername STOP "NIAIServ"
sc \\servername STOP "Virtual Server"

Code



Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.