Building an IIS website in PowerShell

A tiny script to create a local website in IIS just so I could test out a set of static html pages to make sure they were correct before checking them in. Creates a named app pool then creates the website (and deletes existing app pool and website if they already exist).

Import-Module WebAdministration

[string]$IISAppPoolName = "my-lovely-site"
[string]$IISAppPoolDotNetVersion = "v4.0"
[string]$IISAppName = "MyLovelySite"
[int]$Port = 3000

[string]$ScriptFile = $MyInvocation.MyCommand.Path
[string]$ScriptFolder = Split-Path $ScriptFile -Parent

[string]$DirectoryPath = Join-Path $ScriptFolder -ChildPath html

cd IIS:\AppPools\

If (Test-Path $IISAppPoolName -PathType container) {
    Remove-WebAppPool $IISAppPoolName

$AppPool = New-Item $IISAppPoolName
$AppPool.managedRuntimeVersion = $IISAppPoolDotNetVersion

cd IIS:\Sites\

If (Test-Path $IISAppName -PathType Container) {
    Remove-Website $IISAppName

New-Website -Name $IISAppName -PhysicalPath $DirectoryPath -ApplicationPool $ -Port $Port | Out-Null

Start-Process -FilePath "http://localhost:$Port/index.html"

Finally, I fire up a browser window to see what the index page looks like.

If you liked this post you can tweet it or follow me on Twitter!