Wishlist 0 ¥0.00

Automating IIS Application Pool Creation and Configuration with PowerShell (Full Script Included)

 

When deploying ASP.NET web applications, configuring the IIS Application Pool correctly is crucial for performance and stability. This article demonstrates how to automate the creation and configuration of an IIS application pool using PowerShell, including setting .NET runtime, pipeline mode, recycling schedules, and other options.


📘 Scenario

Let’s say we are deploying a website store.fictivesite.com, located at:

D:\Websites\store.fictivesite.com\wwwroot

We want the associated application pool to meet the following requirements:

  • Use .NET CLR v4.0

  • Use Integrated pipeline mode

  • Automatically start and stay always running

  • Recycle the pool four times a day at:

    • 02:00 AM

    • 08:00 AM

    • 12:00 PM

    • 07:30 PM

  • Max request queue length: 1000

  • Max processes: 1

  • CPU throttling disabled, but rapid-fail protection enabled


🔧 PowerShell Script

Here's a complete script you can run in PowerShell to configure everything:

Import-Module WebAdministration

# Define the app pool name
$appPool = "store.fictivesite.com"

# Remove it if it already exists
if (Test-Path IIS:\AppPools\$appPool) {
    Remove-WebAppPool -Name $appPool
}

# Create a new application pool
New-WebAppPool -Name $appPool

# Basic settings
Set-ItemProperty IIS:\AppPools\$appPool -Name managedRuntimeVersion -Value "v4.0"
Set-ItemProperty IIS:\AppPools\$appPool -Name managedPipelineMode -Value "Integrated"
Set-ItemProperty IIS:\AppPools\$appPool -Name autoStart -Value $true
Set-ItemProperty IIS:\AppPools\$appPool -Name startMode -Value "AlwaysRunning"
Set-ItemProperty IIS:\AppPools\$appPool -Name queueLength -Value 1000
Set-ItemProperty IIS:\AppPools\$appPool -Name "processModel.maxProcesses" -Value 1
Set-ItemProperty IIS:\AppPools\$appPool -Name "failure.rapidFailProtectionInterval" -Value "00:05:00"
Set-ItemProperty IIS:\AppPools\$appPool -Name "cpu.limit" -Value 0
Set-ItemProperty IIS:\AppPools\$appPool -Name "cpu.action" -Value "NoAction"

# Set recycling schedule times
$times = @("02:00:00", "08:00:00", "12:00:00", "19:30:00")
foreach ($time in $times) {
    Add-WebConfigurationProperty `
        -pspath "MACHINE/WEBROOT/APPHOST" `
        -filter "system.applicationHost/applicationPools/add[@name='$appPool']/recycling/periodicRestart/schedule" `
        -name "." -value @{value=$time}
}

Write-Host "Application Pool '$appPool' has been created and configured successfully." -ForegroundColor Green

✅ How to Run

  1. Save the script as Create-AppPool.ps1

  2. Open PowerShell as Administrator

  3. Run:

    Set-ExecutionPolicy RemoteSigned -Scope Process
    .\Create-AppPool.ps1
    

✅ Example Output

Upon successful execution, PowerShell will output:

Name                     State        Applications
----                     -----        ------------
store.fictivesite.com    Started
Application Pool 'store.fictivesite.com' has been created and configured successfully.

This confirms that your new app pool is up and running with the correct settings.


🛠 Common Error and Fix

You might see this error if you use the wrong path for setting the schedule:

Add-WebConfigurationProperty : The object at path 'IIS:\AppPools\xxx' does not define configuration.

This happens because IIS:\AppPools does not support deep configuration like recycling schedules.

Fix: Use this instead:

-pspath "MACHINE/WEBROOT/APPHOST"

This targets the actual applicationHost.config, where recycling schedules reside.


🧩 Conclusion

Using PowerShell to automate IIS application pool creation ensures consistent configuration and saves time compared to manual setup via the IIS GUI. You can include this script in your deployment process or CI/CD pipeline for repeatable environments.

If you'd like to go further and also create the website (New-Website), bind to a domain, and link it to this app pool, let me know—I'm happy to extend the script for full deployment automation.

No comments

About Us

Since 1996, our company has been focusing on domain name registration, web hosting, server hosting, website construction, e-commerce and other Internet services, and constantly practicing the concept of "providing enterprise-level solutions and providing personalized service support". As a Dell Authorized Solution Provider, we also provide hardware product solutions associated with the company's services.
 

Contact Us

Address: No. 2, Jingwu Road, Zhengzhou City, Henan Province

Phone: 0086-371-63520088 

QQ:76257322

Website: 800188.com

E-mail: This email address is being protected from spambots. You need JavaScript enabled to view it.