close
AppPool-Restart4

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


Tags : iispowershell
Byron Pate

The author Byron Pate

  • UPDATE – I found a bug in the original command. If you have a stopped App Pool on the server, any App Pool that is newer than the stopped App Pool will not get piped into the recycle command. To get past that, use the /state:Started filter in the list command. I’ve updated the post to reflect that.

  • Awesome. Thanks man!

  • Curious.. for situations where I do not want to recycle all app pools.. would it be possible to modify the script to enumerate all the app pools and then allow you to select which to recycle? Otherwise I would need a separate script for each application pool which sucks 🙂

  • I could think of two ways to do what you were asking. I updated the main post with the two scripts. I think the first one is more what your thinking, and the second one is more what I was thinking 😉 Good luck!

  • Thanks… that helps.. I guess I was thinking more of a multiple choice option where I choose a particulate number for the app pool I want to restart rather than having to choose y/n for each prompt.

    For example..

    1 – To restart app pool A
    2 – To restart app pool B
    3 – To restart app pool C
    etc

    Also, the second one error out with..

    Missing closing ‘)’ in expression.

  • Take a look at the script I just added. I think its what you’re thinking of. A fun way to use Hash Tables in Powershell.

  • Restarting DefaultAppPool
    The term ‘Restart-WebAppPool’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, v
    erify that the path is correct and try again.
    At line:31 char:31
    + Restart-WebAppPool <<<< $val
    + CategoryInfo : ObjectNotFound: (Restart-WebAppPool:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Complete

  • Check that this is executing correctly “Import-Module WebAdministration”. Restart-WebAppPool is a cmdlet from the IIS Administration Module.

    Worst case use appcmd, replace “Restart-WebAppPool $val” with “& $env:windir\system32\inetsrv\appcmd recycle apppool $val”

  • Grzesiek

    Hello,

    is there a way to just restart app pools which related application/websites has Started/Running status using only Powershell commands?

    Thanks