IIS App Pool Recycle – PowerShell + Task Scheduler

1. PowerShell Script

  • File path: C:\Scripts\RecycleAppPool.ps1

  • Purpose: Recycles the IIS Application Pool test.example.com.

  • Script contents:

    Import-Module WebAdministration
    Restart-WebAppPool -Name "test.example.com"

2. Scheduled Task Setup

Action

  • Executes PowerShell with parameters to run the script silently.

$action = New-ScheduledTaskAction `
    -Execute "powershell.exe" `
    -Argument "-NoProfile -WindowStyle Hidden -File C:\Scripts\RecycleAppPool.ps1"

Trigger

  • Starts once (1 minute from creation).

  • Repeats every 1 minute for 1 day.

  • Effectively keeps running every minute, renewed daily.

Register the Task

  • Task name: RecycleAppPool_Test.

  • Runs with highest privileges under the SYSTEM account.

3. Notes & Recommendations

  • Frequency: Current setup recycles the app pool every 1 minute. ⚠️ This is unusual and may cause performance issues (sessions dropped, caches reset).

  • Best Practice: Recycle once daily during off-peak hours (e.g., 3 AM).

  • Logs/Monitoring:

    • Check Task Scheduler → Task History to confirm successful execution.

    • IIS logs will show recycle events.


REFERENCES

Last updated

Was this helpful?