Quick IIS App Pool Recycle

App Pool Recycle1

Powershell One Liner to do a Quick IIS App Pool Recycle

As I mentioned on a previous post, doing an IIS Reset is the worse thing you can do to an IIS Environment. 99% of the time you just need to tear down the AppDomain and load a new one. Performing an IIS Reset does that and also shuts down your HTTP Server, causing clients to receive TCP connection errors. Here’s a quick Powershell one liner to recycle all the Application Pools in IIS7 or IIS8. The command pipes the current list of App Pools in XML format into a recycle command. This is good for code deployment scripts or if you need a scheduled customized restart script.

Write-Host ""
Write-Host "Starting App Pool Recycle...." -ForegroundColor Magenta

& $env:windir\system32\inetsrv\appcmd list apppools /state:Started /xml | & $env:windir\system32\inetsrv\appcmd recycle apppools /in

Write-Host "Recycle Complete" -ForegroundColor Magenta
Write-Host ""
Recycle All Application Pools

Of course, as with any IT technique there’s 100 other ways to do it. But this has been one of my favorites. Read up on AppCmd for other ideas, or the WebAdministration Powershell Module.




Update 8/13

JJDurant posted a good question about how to selectively restart Application Pools. I guess there’s two ways to handle this, you could prompt the user if they want to restart each app pool. Or you could make the script accept the app pool name and then pass it to the /AppPool.Name paramenter. I like the second way better myself, but if you didn’t know all the pool names the first way could be useful.

Update 8/14

I added a third script that lists all the Application Pools and lets the user select the Pool to restart.


Import-Module WebAdministration -ErrorAction SilentlyContinue

$pools = gci iis:\apppools

foreach ($a in $pools ){
    $pool = $a.Name

    Write-Host ""
    $answer = read-host "Restart $pool ? y/n"

    if ($answer -eq "y")
    {
    	write-host "Restarting $pool"
    	Restart-WebAppPool $pool
    }
}    

Write-Host "Restart Complete"
Write-Host ""

Prompt the User to Restart

param(
 [Parameter(Mandatory=$true)]
 [String]$poolname
)

Write-Host ""
Write-Host "Starting App Pool Recycle of $poolname...." -ForegroundColor Magenta

& $env:windir\system32\inetsrv\appcmd list apppools /apppool.name:$poolname /xml | & $env:windir\system32\inetsrv\appcmd recycle apppools /in

Write-Host "Recycle Complete" -ForegroundColor Magenta
Write-Host ""

Recycle A Single Application Pool

Import-Module WebAdministration -ErrorAction SilentlyContinue

$pooltable = @{} 
 
$pools = gci iis:\apppools
$i = 0

Write-Host "Select Application Pool to Restart"
Write-Host ""

foreach ($a in $pools ){
    $pool = $a.Name
	$i += 1
	$pooltable.Add($i, $pool)
    Write-Host "$i – To restart app pool $pool"
}

Write-Host ""
$answer = read-host "Enter # of Application Pool to Restart "

if ($answer)
{
	foreach ($h in $pooltable.GetEnumerator()) {
    	$key = $h.Name
		$val = $h.Value
		if ($key -eq $answer)
		{
			Write-Host "Restarting $val"
			Restart-WebAppPool $val
		}
	}
}

Write-Host "Complete" -ForegroundColor Magenta
Write-Host ""

Multiple Choice Recycle


My New Stories

March 2016 Web Hosting Deals
Powershell AD Group Management
Troubleshooting 403