Use SC as a more fully functioning Net command alternative
Command-line junkies love the ability to manipulate Windows services from the command prompt, but until a few years ago all we had was Net Start, Net Pause, Net Continue, and Net Stop, which—as their names suggest—start, pause, continue (i.e., "un-pause"), and stop a service.
If you'd like to be able to do more with services from the command line, you should check out the SC tool (sc.exe), which you'll find in Windows XP and later.
What Does It Look Like?
SC commands share the structure
sc \[\\<servername>\] <command> <servicename> \[<options>\]
where the optional servername simply points to the system at which to direct the command, command is the particular SC subcommand (e.g., pause, start, create, delete), servicename is the service's internal name, and options refers to any options that the subcommand requires. The simple example
sc \\server1 stop lanmanserver
contacts the Server1 system and stops the Server service, which goes by the internal service name lanmanserver. If I wanted to stop the Server service locally, I'd simply drop the servername option.
The Internal Name
Knowing the service's internal name is essential. If I assume that the displayed name for the file-server service is Server, and I try the command
sc stop server
I get an SC error. Only lanmanserver works. The internal name is also called the key name because lanmanserver is the name of the registry subkey within HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services in which Windows stores information about that particular service. If you know the friendlier display name— such as Server for the file-server service or DNS for the DNS Server service—just use the getkeyname command to convert a display name to a key name. For example, to find the DNS Server service's key name, I could use the command
sc getkeyname "dns server"
Doing so tells me that the DNS Server service's key name is DNS. (Case seems not to matter to SC, by the way.) You can reverse the process, and—given the key name—get the more human-friendly display name, by using the getdisplayname command.
How Does It Work?
SC has four commands that correspond to the aforementioned Net commands: start, stop, pause, and continue. For example, to pause the server service, I could type
sc pause server
SC's "stop" functionality behaves a little differently than it does with the Microsoft Management Console (MMC) Services snap-in or the Net Stop command, which understand that the Computer Browser service depends on the Server service. The Services snap-in and Net Stop ask if it's OK to stop the Computer Browser service as well, whereas SC just fails with the statement A stop control has been sent to a service that other running services are dependent on. In other words, in this instance, SC isn't quite as smart as the other commands.
With the enumdepend command, however, you can determine what other services depend on a given service:
sc enumdepend lanmanserver
In response, SC reports that the DFS and Browser services depend on the Server service. But SC returns this information in a sort of terse, techie way. For example, you can use the query command to query a service about its current state. Querying the Server service with
sc query lanmanserver
returns the information that Figure 1 shows. This is the basic way that SC lists information about a service.
SC is a pretty useful tool, but I've only just begun. Join me next month for more SC!