How to Start, Stop, Recycle IIS application pool programmatically?

IIS application pool defines a set of web applications and their corresponding worker processes to provide a convenient way to administer a set of web sites and applications. Sometime developers need to programmatically start, stop or recycle the IIS application pool.


In this post, we will learn how to programmatically call the directory service to invoke the start, stop and recycle operation on IIS server.


Like all websites present in IIS server, the application pool also has a specific URL. It uses “IIS:” protocol to communicate with the server. The URL consists of SERVER_NAME, a generic path and the name of the application pool.


Here is the structure of the application pool URL (don’t forget to change the SERVER_NAME and APP_POOL_NAME):




Application Pool in IISTo load the pool, you need to use the class “DirectoryEntry” present under the namespace “System.DirectoryServices” which encapsulates a node or object in the Active Directory Domain Services hierarchy. It has a method called Invoke(…), which calls the native Active Directory Domain Services object.


The Invoke(…) method takes the method name as the parameter which you want to call on that domain service object. The second parameter takes arguments to that method. If you want to stop the Application pool, set the first parameter to “Stop”. In case of start or recycle, just use “Start” or “Recycle” respectively.


Here is a code snippet that will guide you creating the DirectoryEntry object and calling the Invoke(…) method with proper parameter to do specific operation with the IIS application pool:


var directoryEntry = new DirectoryEntry(APPLICATION_POOL_URL, USERNAME, PASSWORD);
// call to stop the Application Pool
directoryEntry.Invoke("Stop", null);
// call to start the Application Pool
directoryEntry.Invoke("Start", null);
// call to recycle the Application Pool
directoryEntry.Invoke("Recycle", null);


Hope this small post will help you if you want to do Start, Stop or Recycle operation programmatically in any of your IIS application pool. Let me know, if you have any queries on that. I am available on Twitter and Facebook. Don’t forget to connect with me.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly.

Don't forget to share your feedback/queries in the comment section below.


  1. What if the website is running as a NetworkService? Is this possible then?

  2. Can be very useful, to check the caches using a windows or web services (in C#), that will recycle the app pools

  3. Good and help full point for interview purpose.

  4. Thank ...

    Its relay help full information for interview.

  5. Hint: you are using DirectoryEntry which by default will not work from IIS7+ unless you have the IIS 6 compatibility mode feature installed.

    From IIS 7 onwards there is a new API available via ServerManager implemented in the Microsoft.Web.Administration.dll in the inetsrv folder.

    So based on this information the modern verison of this implementation working with IIS 7/8 would look something like this:

    public void PoolRecycle(string appPoolName)
    using (var serverMgr = new ServerManager())
    var appPool = serverMgr.ApplicationPools.SingleOrDefault(p => p.Name.Equals(appPoolName, StringComparison.InvariantCultureIgnoreCase));
    if (appPool != null)
    Trace.WriteLine(string.Format("[PoolRecycle] Can't find pool: [{0}]", appPoolName));

    1. Thank you Csaba Harmath. I will check out that today.

  6. It is showing error to me.

    Access is denied.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Runtime.InteropServices.COMException: Access is denied.

    [COMException (0x80070005): Access is denied.
    System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439385
    System.DirectoryServices.DirectoryEntry.Bind() +36
    System.DirectoryServices.DirectoryEntry.get_NativeObject() +31
    System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) +53

  7. I see the new ServerManager, but how do I use this to connect to a remote web server? Thanks


© 2008-2018 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top