Powershell | Mastering IIS using Powershell

It is always a very mechanical job to configure the Microsoft Internet Information Services (IIS) to setup an environment. It is very time consuming to configure your websites, bindings, application pools, web applications and their properties and attributes respectively. Here comes powershell for rescue, where we can automate few or rather many of our IIS configuration tasks.

Let’s start with creating Web Application Pool.

New-WebAppPool "TestWebAppPool"

Above script will create new Web Application Pool with default properties:

IIS Default Properties

Below are few commands for Web Application Pool properties that we may need to configure every now and then:

Import-Module WebAdministration
$pool  = Get-ChildItem IIS:\AppPools | where { $_.name -eq  "TestWebAppPool"} | Select-Object -First 1

$pool.managedRuntimeVersion = "v2.0"
# Possible Values
# ''     No Managed Code
# v4.0   DotNet Framework
# v2.0   DotNet Framework

$pool.enable32BitAppOnWin64 = "true"
# Possible Values
# true
# false

$pool.managedPipelineMode = 1
# Possible Values
# 0    Integrated
# 1    Classic

$pool.processmodel.identityType = 3
# Possible Values
# 0    LocalSystem
# 1    LocalService
# 2    NetworkService
# 3    Custom User
# 4    ApplicationPoolIdentity

# For IdentityType Custom User
$pool.processmodel.username = "UserName"
$pool.processmodel.password = "Password"

$pool | Set-Item

Output of above script is below:

IIS Modified Properties

Now let’s look at commands to create and configure websites.

New-Website -Name "TestWebsite" -Force

Please Note: You will need to use switch “-Force” as physical path is required field.

Above script will create new Website with default properties: (Note there is no path of the website and website is stopped)

Default Website Properties

More specific command as per one’s need:

New-Website -Name "TestWebsite" -Port "92" -PhysicalPath "C:\TestWebSite" -ApplicationPool "TestWebAppPool" 

Creating secure (HTTPS) website:

New-Website -Name "TestWebsite" -Port "443" -PhysicalPath "C:\TestWebSite" -ApplicationPool "TestWebAppPool" -Ssl

Below are few commands for Website properties that we may need to configure every now and then:

$webSite  = Get-Item IIS:\Sites\TestWebsite

$webSite | Set-ItemProperty -Name "applicationPool" -Value "NewApplicationPoolName"
$webSite | Set-ItemProperty -Name "physicalPath" -Value "WebSitePhysicalPath"

Adding certificate to your HTTPS binding:

(Get-ChildItem cert:\LocalMachine\My | where-object { $_.Subject -eq "Some Certificate Name" } | Select-Object -First 1).Thumbprint
$binding = Get-WebBinding -Name "TestWebsite" -Protocol "https"
$binding.AddSslCertificate($cert, "my");

Let’s look at the command to create new Web Application.

New-WebApplication -Name "TestWebApplication" -Site "TestWebsite" -PhysicalPath "SomeWebApplicationPath" -ApplicationPool "TestWebAppPool"

Small handy script to update the password for all App Pool using Custom User as Identity Type:

Import-Module WebAdministration
$UserName = "UserName"
$Password = "Password"
$applicationPools = Get-ChildItem IIS:\AppPools | where { $_.processModel.userName -eq  $UserName}

foreach($pool in $applicationPools)
    $pool.processModel.userName = $UserName
    $pool.processModel.password = $Password
    $pool.processModel.identityType = 3
    $pool | Set-Item

Delete Web Application

Remove-WebApplication -Name "TestWebApplication" -Site "TestWebsite"

Delete Website

Remove-Website -Name "TestWebsite"

Delete Web Application Pool

Remove-WebAppPool -Name "TestWebAppPool"

There is lot more to powershell, but I have tried to present just few of mostly used commands. Deep dive into it and you will surely fall in love with powershell.

More and complete information on powershell is here

Leave a Reply

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