Click here to Skip to main content
15,355,301 members
Articles / Programming Languages / C#
Posted 31 May 2003


97 bookmarked

Command Line Windows Services Manager

Rate me:
Please Sign up or sign in to vote.
4.69/5 (32 votes)
23 Jun 20033 min read
How to manipulate Windows services on local or remote computers.

Image 1

Image 2


The .NET Framework provides easy access to the service control manager for manipulating Windows services. In this article, I will show you the command line service manager tool that I developed using these features.


The topic of managing Windows services is covered on MSDN at Monitoring Windows Services. It is also discussed on CodeProject in Agus Kurniawan's article Windows Service Management.

Mine differs from Agus's in the following areas:

  1. Agus provides a UI solution, similar to the service control panel, while mine uses a command line.
  2. Agus covered the basics of service management including starting and stopping services, while mine tries to cover areas like starting, stopping, pausing, continuing and restarting services.
  3. Mine also tries to address the timeout and impersonation issue when managing Windows services.

Basics in Windows service management

In .NET, Windows service management is achieved by the System.ServiceProcess.ServiceController. The following code demonstrates how to list all the services on the machine srv2:

using System.ServiceProcess;

public class SvcMgr {

   public static void Main( string[] args ) {
       ServiceController[] services = 
                          ServiceController.GetServices( "srv2" );
       foreach( ServiceController service in services ) {
           Console.WriteLine( string.Format( "{0} [ {1} ]",
               service.ServiceName, service.Status.ToString() ) );

How to handle timeouts

Timeouts are handled by calling the WaitForStatus method on the service controller. The following code demonstrates how to start the "IISADMIN" service on srv2 using 10 second timeout:


ServiceController service = 
    new ServiceController( "IISADMIN", "srv2" );

TimeSpan t = TimeSpan.FromSeconds( "10" );
     ServiceControllerStatus.Running, t );


When a timeout happens, a System.ServiceProcess.TimeoutException will be thrown. The command line tool that I developed handles this in the following way:

Image 3

How to handle impersonation when managing services

The account that you use to manage Windows services needs the privilege to access the service control manager on the target machine; otherwise, you'll get a System.InvalidOperationException wrapped with a System.SystemException, stating, "Cannot open Service Control Manager on computer 'srv3'. This operation might require other privileges."

Image 4

Note: This exception represents the fact that the service control manager on the target machine is not accessible. This could mean the current account that you use does not hold enough privileges or simply the machine is offline.

This is where impersonation comes into picture. By impersonating a different Windows logon which has the privilege to access service control manager, we can continue without having to logon as a different user. See how impersonation works in this command line service manager:

Image 5

Note: The account that you are trying to impersonate has to be both in the local system and in the target machine, otherwise the program won't know whom to impersonate. To see how impersonation was implemented in this tool, please see the file SvcMgr.cs in the source code.

For more information on what impersonation is and how impersonation works, please refer to Implementing Impersonation in an ASP.NET Application on MSDN.

Putting it all together

This Command Line Service Manager utility puts all of the above topics together and is a useful tool for managing Windows services on a local or remote machine(s).

Things that are missing

This command line tool does not have the capability to change the default startup type of a service. If somebody is interested in looking further, I suggest this is the direction to dive in, although it seems ServiceController does not provide such functionalities.



  • 06/24/2003
    • Document modified.
  • 05/24/2003
    • Document written.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Haidong Chen
Web Developer
United States United States
I'm an IT Professional from New York.

Personal website -

Comments and Discussions

Generalnice job! Pin
philippdev21-Jun-13 22:22
Memberphilippdev21-Jun-13 22:22 
GeneralWindows Service Manager Tray software, similar to this Pin
logicalregistrator1-Jul-09 2:05
Memberlogicalregistrator1-Jul-09 2:05 
GeneralThanks Pin
Youzh7-Oct-08 1:43
MemberYouzh7-Oct-08 1:43 
GeneralWindows service is not starting through remote machine in WORKGROUP Pin
Ananthraj28-May-07 15:10
MemberAnanthraj28-May-07 15:10 
GeneralRe: Windows service is not starting through remote machine in WORKGROUP [modified] Pin
kiquenet.com29-Sep-08 21:02
professionalkiquenet.com29-Sep-08 21:02 
Generalit didnot work in my computer too Pin
vuchihieu2-Feb-07 1:29
Membervuchihieu2-Feb-07 1:29 
QuestionIssue! Pin
Moin Ahmed6-Nov-06 23:56
MemberMoin Ahmed6-Nov-06 23:56 
GeneralIntegrate window service with any web application Pin
Faheem Habib25-Aug-05 1:35
MemberFaheem Habib25-Aug-05 1:35 
GeneralWORK!! but if Password containd blank space does not work...WHY!!! Pin
Brian Paul28-Jun-05 8:06
MemberBrian Paul28-Jun-05 8:06 
GeneralCould this be implemented for WindowsCE Pin
mkearl4-Apr-05 23:00
Membermkearl4-Apr-05 23:00 
QuestionExecuteCommand? Pin
Kocha16-Jun-04 21:09
MemberKocha16-Jun-04 21:09 
GeneralThis doesn't work for me Pin
George Nunn3-May-04 1:57
MemberGeorge Nunn3-May-04 1:57 
Generalsimply excellent work!! Pin
abc87619-Jul-03 5:26
Memberabc87619-Jul-03 5:26 
GeneralRe: simply excellent work!! Pin
Skorunka2-Dec-08 10:14
MemberSkorunka2-Dec-08 10:14 
GeneralStartup Types Pin
Heath Stewart24-Jun-03 5:14
protectorHeath Stewart24-Jun-03 5:14 
GeneralExcellent Work Pin
Jason Gerard3-Jun-03 3:08
MemberJason Gerard3-Jun-03 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.