Things you can do by piping appcmd commands

Here are some of the cool things you can do by piping appcmd commands together in IIS7.

Application Pools
1. Recycle all application pools (replace recycle with start/stop to start/stop all apppools)
appcmd list apppool /xml | appcmd recycle apppool /in

2. Stop application pools with word "cheap" in it
appcmd list apppool /name:"$=*cheap*" /xml | appcmd stop apppool /in


3. Set property enable32BitAppOnWin64 to true for all apppools (Filter apppools as in 2 if needed)
appcmd list apppool /xml | appcmd set apppool /in /enable32BitAppOnWin64:true

4. Start apppools which are stopped
appcmd list apppool /state:Stopped /xml | appcmd start apppool /in

5. Recycle application pools which are used in some applications
appcmd list app /xml | appcmd list apppool /in /xml | appcmd recycle apppool /in
appcmd list app /xml | appcmd recycle apppool /in (This might recycle one apppool multiple times)

6. Recycle apppools serving website “Default Web Site”
appcmd list site "Default Web Site" /xml | appcmd list app /in /xml | appcmd list apppool /in /xml | appcmd recycle apppool /in
appcmd list app /site.name:"Default Web Site" /xml | appcmd list apppool /in /xml | appcmd recycle apppool /in


Sites
7. Start all the sites (Replace start with stop to stop all sites)
appcmd list site /xml | appcmd start site /in

8. Start the sites which are stopped
appcmd list site /state:stopped /xml | appcmd start site /in

9. Set serverAutoStart to false for all sites
appcmd list site /xml | appcmd set site /serverAutoStart:false /in

10. Keep sites config data and restore later
appcmd list site /config /xml > sites.xml
appcmd add sites /in < sites.xml


Applications and Vdirs
11. Delete all apps which are using a particular apppool
appcmd list apppool DefaultAppPool /xml | appcmd list app /in /xml | appcmd delete app /in

12. Move all applications in a site to NewAppPool apppool
appcmd list app /site.name:"Default Web Site" /xml | appcmd set app /in /applicationPool:NewAppPool

13. List all sites with "/test" app
appcmd list app /path:"/test" /xml | appcmd list site /in

14. List apps created by user10 (assuming all his apps under a folder whose name contains user10)
appcmd list vdir /physicalPath:"$=*user10*" /xml | appcmd list app /in

15. List sites which read from C:\inetput\wwwroot
appcmd list vdir /physicalPath:C:\inetput\wwwroot /xml | appcmd list app /xml /in | appcmd list site /in

16. List the vdirs of sites which are stopped
appcmd list site /state:stopped /xml | appcmd list app /xml /in | appcmd list vdir /in

Worker processes and Requests
17. Stop apppools of requests running for more than 60 seconds
appcmd list request /xml /time:"$>60000" | appcmd list apppool /in /xml | appcmd stop apppool /in

18. List apps served by wp 3600
appcmd list wp 3600 /xml | appcmd list apppool /xml /in | appcmd list app /in

Modules
19. Disable all managed modules
appcmd list module /preCondition:managedHandler /xml | appcmd delete module /in

20. Uninstall all native modules
appcmd list module /type:"" /xml | appcmd uninstall module /in

21. Unlock all module entries under system.webServer/modules (won’t work on vista)
appcmd list module /xml | appcmd set config /lockItem:false /in


Configuration
22. Keep config of a particular section and restore later
appcmd list config http://localhost/app1/ /section:caching /xml /config > config.xml
appcmd set config
http://localhost/app1 /in < config.xml


Backups and Traces
23. Delete all backups
appcmd list backup /xml | appcmd delete backup /in


24. List sites generating 404
appcmd list trace /statusCode:404 /xml | appcmd list site /in


Hope this helps.

-Kanwal

10 Comments

  • Great post - I've been meaning to cover this myself for too long! This should be very useful for server admins ...

    Mike

  • great post! Very cool scenarios that most people probably don't realize AppCmd is capable of.

  • so is powershell beneath it? If it has to work with ServerCore and then possibly that is not possible.

  • Govind, appcmd is not using powershell for this. It has its own implementation and so will work on servercore as well.

  • great post! Very cool scenarios that most people probably don't realize AppCmd is capable of

  • Hi Kanwal,

    First, thank you very much for posting all these examples! It is very helpful.

    I seem to be having trouble adding a virtual directory to each IIS site. I thought this should work:

    appcmd list site -xml | appcmd add vdir -in /path:/mydir /physicalpath:c:\mydir

    I thought SITE.NAME, from "list site", would automatically be used as the required app.name, in "add vdir". It is not.

    Was the task of 'adding a virtual directory to each site' considered, in the creation of the appcmd tool?

    If so, could you please share with me how to do this? If not, could it please be considered for the next version of appcmd? I undestand Mike created this tool, and is no longer with Microsoft.. so, I'm not sure if this tool will get any updates..

    I've also posted this question to forums.iis.net.

    Thanks very much, in advance for your time!,
    -Aaron

  • Hi Kanwal,

    That forums.iis.net URL is: http://forums.iis.net/p/1176391/1974795.aspx#1974795

    Thanks,
    -Aaron

  • I need to find a way to check if a Web Site and it's AppPool already exist. If so, then I need to install/update the Web Apps within the Web Site and recycle the App Pool. If they site and pool do not exist, then I need to create them, deploy the applications and start the App Pool. Below is what I've been trying to do in a .cmd file in IIS7. The problem is if I give it a website that doesn't exist, it still hits the "echo %_WebSiteName% Found!"

    @echo off
    cls
    echo.
    :NextArgument
    set _ArgCurrentOriginal=%*
    set _ArgCurrent=%~1

    echo _ArgCurrentOriginal=%_ArgCurrentOriginal%
    echo _ArgCurrent=%_ArgCurrent%
    echo.

    set _WebSiteName="%_ArgCurrent%"
    echo _WebSiteName=%_WebSiteName%
    echo.


    echo appcmd list site %_WebSiteName% /text:name
    echo.

    set _AppCmdLine=appcmd list site %_WebSiteName% /text:name

    call %_AppCmdLine% > websitename.txt
    echo list file content
    type websitename.txt
    echo.

    echo Load file content
    set _FoundName=websitename.txt
    echo.

    echo _FoundName="%_FoundName%"
    echo.

    IF /I NOT("%_FoundName%"=="") (
    echo %_WebSiteName% Not Found!
    ) else (
    echo %_WebSiteName% Found!
    )
    echo.

    pause

  • Hi Kanwal,

    Does appcmd's 'set config' support the '/in' switch? As in this example:

    C:\Windows\system32\inetsrv\appcmd.exe list site /xml | C:\Windows\system32\inetsrv\appcmd.exe set config /in /section:isapiFilters /+"[name='MyFilter',path='C:\my_filter.dll']"

    I've also posted this question on Mike's blog: http://mvolo.com/blogs/serverside/archive/2007/06/19/Do-complex-IIS-management-tasks-easily-with-AppCmd-command-piping.aspx?CommentPosted=true#commentmessage

    I've also posted this question in the IIS.net forums: http://forums.iis.net/p/1182209/1998492.aspx#1998492

    Thanks in advance for any help you might be able to offer,
    -Aaron Neff

  • Does anyone know how to change the order of existing ISAPIFilters using appcmd? GUI equivalent is to use "View Ordered List" and use Move Up and Down.

Comments have been disabled for this content.