IIS – create Web Site using PowerShell

In this article i’ll quickly document ‘how to create a web site using PowerShell’.

IISAdministration vs WebAdministration Modules

You have two options for managing IIS. You can use WebAdministration or IISAdministration. But which one should you use?

If you’re using IIS IIS 10.0 or higher, the newer IISAdministration module offers a lot of improvements over the older WebAdministration module.

Here are a few highlights:

  • IISAdministration cmdlets offer better support for the pipeline and scale better.
  • Compared to the older, legacy IIS WebAdministration cmdlets, IISAdministration contains more efficient code.
  • IISAdministration cmdlets are simpler, and provide direct access to the server manager.

This article is going to focus on this newer IISAdministration module.

Prerequisites

Prerequisites

This article will be hands-on.
Make sure you have the following prerequisites in place.

Installing the IISAdministration Module

Open up an elevated PowerShell console on your web server and install the module using the command below.

Install-Module IISAdministration -Force

Creating a Web Site

Create a directory

New-Item -ItemType Directory -Name HelloWorldWebSite -Path C:\inetpub\

Create a file named default.asp

New-Item -ItemType File -Name default.aspx -Path C:\inetpub\HelloWorldWebSite\

Once the file is created, open it up in your favorite editor and copy and paste the code below into that document.

notepad C:\inetpub\HelloWorldWebSite\default.aspx

Copy/paste the content from this article into the default.asp

Save the default.asp

Create the new Web Site

New-IISSite -Name 'HelloWorldWebSite' -PhysicalPath 'C:\inetpub\HelloWorldWebSite\' -BindingInformation "*:8080:"

Add default.aspx to default document handler

$filter = "system.webserver/defaultdocument/files"
$site = "IIS:\Sites\HelloWorldWebSite"
$file = "default.asp"
if ((Get-WebConfiguration $filter/* "$site" | where {$_.value -eq $file}).length -eq 1)
{
   Remove-WebconfigurationProperty $filter "$site" -name collection -AtElement @{value=$file}
}
Add-WebConfiguration $filter "$site" -atIndex 0 -Value @{value=$file}