How to – Create multiple IIS sites from text file using Powershell

The end result is the following script which will import the contents of a .txt file from c:\rs-pkgs\websites.txt by default. Simply populate your text file with each site name on a separate line, e.g.:

test1.com

test2.com

test3.com

test4.com

Save that in C:\rs-pkgs as websites.txt and then run the following script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Import-Module WebAdministration
$i = 0
ForEach ($Item in Get-Content -Path C:\rs-pkgs\website.test.txt)
{
    $iisAppPoolName = [string]$Item
    $iisAppPoolDotNetVersion = "v4.0"
    $iisAppName = $iisAppPoolName
    $directoryPath = "C:\inetpub\wwwroot\" + $iisAppPoolName
    #navigate to the app pools root
    cd IIS:\AppPools\
    #check if the app pool exists
    if (!(Test-Path $iisAppPoolName -pathType container))
    {
        #create the app pool
        $appPool = New-Item $iisAppPoolName
        $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
    }
    #navigate to the sites root
    cd IIS:\Sites\
    #check if the site exists
    if (Test-Path $iisAppName -pathType container)
    {
        return
    }
    #create the directory
    md $directoryPath
    #create the site
    $iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" + $iisAppName} -physicalPath $directoryPath
    $iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName
    $i += 1
}

from https://community.rackspace.com/developers/f/7/t/1735