Batch file to create an IIS7 website

Really simple stuff which is helping me out when hosting multiple sites for development on one machine; you either pass in as parameters or specify as responses

  1. the directory name of your site – e.g. “D:\Dev\MySite1” would be “MySite1”
  2. the port number you want it on
  3. the site ID

and it’ll set up the site, migrate your config settings to II7 if necessary, start the new site and let you know the URL to access it.

@echo off
setlocal EnableDelayedExpansion

REM Get parameters from user if they're not specified
if [%1]==[] set /P directory="Enter name of directory/site: "
if [%2]==[] set /P port="Enter port number: "
if [%3]==[] set /P sitenum="Enter site number: "

REM Create site in IIS
%systemroot%\system32\inetsrv\appcmd add site /name:"%directory%" /id:%sitenum% /physicalPath:"D:\Dev\%directory%" /bindings:http/*:%port%:%computername%

REM Attempt to migrate config to IIS7 stylee
%SystemRoot%\system32\inetsrv\appcmd migrate config "%directory%/"

REM Start new site
%SystemRoot%\system32\inetsrv\appcmd start site "%directory%"

echo site "%directory%" now running at http://%computername%:%port%

REM interactive mode
if [%1]==[] (if [%2]==[] (if [%3]==[] (
    pause
    exit
)))

I will change this to pull the next available site ID and port number unless someone else can tell me how to do that.

And yes, this would be very easy in Powershell but I’ve not done that version either..!

Also, if you’d like to know how I managed to get Syntaxhighlighter to work nicely with batch/cmd/dos, leave a comment. There are *no* nice, simple, tutorials out there with common mistakes, so I could paste my steps in here if necessary.

3 thoughts on “Batch file to create an IIS7 website

  1. 1. omit site id and it will take the next free number
    2. command line arguments wont work
    3. /bindings:http/*:80:%directory%

    • This one works for me.
      I fixed port to 80 and added a webroot variable
      I also changed binding to site name.

      @echo off
      setlocal EnableDelayedExpansion

      REM assign arguments to variables
      if NOT [%1]==[] set webroot=%1
      if NOT [%2]==[] set directory=%2

      REM Get parameters from user if they're not specified
      if [%1]==[] set /P webroot="Enter path of webroot (slash at the end): "
      if [%2]==[] set /P directory="Enter name of directory/site: "

      REM Create site in IIS
      %systemroot%\system32\inetsrv\appcmd add site /name:"%directory%" /physicalPath:"%webroot%%directory%" /bindings:http/*:80:%directory%

      REM Attempt to migrate config to IIS7 stylee
      %SystemRoot%\system32\inetsrv\appcmd migrate config "%directory%/"

      REM Start new site
      %SystemRoot%\system32\inetsrv\appcmd start site "%directory%"

      echo site "%directory%" now running at http://%directory%:80

      REM interactive mode
      if [%1]==[] (if [%2]==[] (
      pause
      exit
      ))

      Thanks!
      And Have A Nice Day!

  2. hi,

    can any one suggest how to restart a particular site hosted at IIS and recycle the app pool o different machine using batch file.. ????

Leave a Reply to Nilesh Chaubey Cancel reply

Your email address will not be published. Required fields are marked *