PowerShell – Recycle All IIS App Pools

PowerShell

With a little help from Shay Levy’s post on Stack Overflow and the MSDN documentation, I added this handy function to my profile to automatically recycle all IIS app pools.

PowerShell
function Recycle-AppPools {
  
    param(
    [string] $server = "3bhs001",
    [int] $mode = 1, # ManagedPipelineModes: 0 = integrated, 1 = classic
    ) 
 $iis = [adsi]"IIS://$server/W3SVC/AppPools"
 $iis.psbase.children | %{ 
   $pool = [adsi]($_.psbase.path); 
   if ($pool.AppPoolState -eq 2 -and $pool.ManagedPipelineMode -eq $mode) {
     # AppPoolStates:  1 = starting, 2 = started, 3 = stopping, 4 = stopped          
     $pool.psbase.invoke("recycle") 
     }
   }

}
PowerShell

Leave a Reply

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