PowerShell IIS Management: Control Web Servers with WebAdministration

PowerShell IIS Management: Control Web Servers with WebAdministration

PowerShell Tips Editor 3 min read
PowerShell IIS Management: Control Web Servers with WebAdministration

Deploying a web application to ten IIS servers by clicking through IIS Manager on each one is slow, error-prone, and unrepeatable. PowerShell IIS management through the WebAdministration module scripts the entire process: create sites, configure application pools, set SSL bindings, and restart services — all from the command line or a deployment pipeline. This post covers every common IIS operation you need to automate web server configuration reliably across your environment.

Import WebAdministration Module

The WebAdministration module ships with IIS and must be imported before using IIS cmdlets. It also exposes the IIS: PSDrive for path-based navigation of the IIS configuration hierarchy:

Import-Module WebAdministration

# Verify the IIS: drive is available
Get-PSDrive -Name IIS

# Navigate like a file system
Get-ChildItem IIS:\Sites
Get-ChildItem IIS:\AppPools
Name     Provider  Root
----     --------  ----
IIS      WebAdmin

List Sites and App Pools

Get-Website and Get-WebAppPool return site and pool objects with their current state:

# List all websites
Get-Website | Select-Object Name, Id, State, PhysicalPath,
    @{N='Bindings'; E={ ($_.Bindings.Collection | ForEach-Object { $_.bindingInformation }) -join ', ' }} |
    Format-Table -AutoSize

# List all application pools with state and .NET version
Get-WebAppPool | Select-Object Name, State,
    @{N='NetVersion';    E={ $_.ManagedRuntimeVersion }},
    @{N='PipelineMode';  E={ $_.ManagedPipelineMode }} |
    Format-Table -AutoSize
Name            State    NetVersion  PipelineMode
----            -----    ----------  ------------
DefaultAppPool  Started  v4.0        Integrated
MyAppPool       Started  v4.0        Integrated
ApiPool         Stopped  No Managed  Integrated

Create a New Website

Create a new website with a physical path and HTTP binding. The -Force switch creates the site even if a site with the same name exists (it updates the existing site):

$siteName    = "CorpIntranet"
$sitePath    = "C:\inetpub\corpintranet"
$bindingInfo = "*:8080:"   # all IPs, port 8080, no hostname

# Create the application pool first
New-WebAppPool -Name "${siteName}Pool"
Set-ItemProperty "IIS:\AppPools\${siteName}Pool" -Name processModel.idleTimeout -Value "00:00:00"

# Create the website
New-Item -Path $sitePath -ItemType Directory -Force | Out-Null
New-Website -Name $siteName -Port 8080 -PhysicalPath $sitePath `
    -ApplicationPool "${siteName}Pool" -Force

Write-Host "Created site '$siteName' on port 8080"

Configure SSL Binding

Add an HTTPS binding to a site by specifying the certificate’s thumbprint. The certificate must already be in the LocalMachine\My store:

$siteName   = "CorpIntranet"
$thumbprint = "A1B2C3D4E5F6A7B8C9D0E1F2A3B4C5D6E7F8A9B0"
$port       = 443
$hostName   = "intranet.corp.com"

# Add the HTTPS binding
New-WebBinding -Name $siteName -Protocol https -Port $port -HostHeader $hostName

# Associate the certificate with the binding
$binding = Get-WebBinding -Name $siteName -Protocol https
$binding.AddSslCertificate($thumbprint, "My")

Write-Host "HTTPS binding configured for $hostName"

Restart Site and App Pool

Restart a specific site and its application pool, or use the IIS: drive for both:

$siteName = "CorpIntranet"

# Stop and restart the site
Stop-Website -Name $siteName
Start-Website -Name $siteName

# Recycle (not restart) the app pool — faster, no downtime
Restart-WebAppPool -Name "${siteName}Pool"

# Stop and start app pool for full restart
Stop-WebAppPool  -Name "${siteName}Pool"
Start-WebAppPool -Name "${siteName}Pool"

# Verify state
Get-Website -Name $siteName | Select-Object Name, State
Get-WebAppPool -Name "${siteName}Pool" | Select-Object Name, State

Check Site Status Remotely

Check the running state of IIS sites and pools on remote servers using Invoke-Command:

$webServers = @('web01', 'web02', 'web03')

$siteStatus = Invoke-Command -ComputerName $webServers -ScriptBlock {
    Import-Module WebAdministration
    Get-Website | Select-Object Name, State,
        @{N='Server';E={$env:COMPUTERNAME}},
        @{N='AppPool';E={$_.ApplicationPool}},
        @{N='PoolState';E={ (Get-WebAppPool -Name $_.ApplicationPool).State }}
} -ThrottleLimit 10

$siteStatus | Where-Object State -ne 'Started' | Format-Table -AutoSize

Common Errors and Fixes

  • WebAdministration requires IIS installed on the machine. The WebAdministration module is only present when the Web Server (IIS) role is installed. Running Import-Module WebAdministration on a non-IIS server throws a module not found error. Install the module remotely with Install-WindowsFeature Web-Scripting-Tools.
  • Web.config changes not reflected until app pool restart. Configuration changes made directly to Web.config files or via Set-WebConfigurationProperty do not take effect until the application pool recycles. Use Restart-WebAppPool after making configuration changes to ensure the application picks them up immediately.

Related Cmdlets / See Also

Wrapping Up

The WebAdministration module combined with the IIS: PSDrive gives you complete scripted control over every IIS configuration setting. Build site creation scripts as part of your deployment pipeline, schedule app pool recycles during off-hours, and use Invoke-Command to push configuration changes across all web servers simultaneously.

Send-Item -To