How do I recycle an IIS App pool with Powershell?

Reference implementation of a Powershell script to recycle app pools, in response to Rick’s post: http://www.west-wind.com/weblog/posts/2012/Oct/02/A-tiny-Utility-to-recycle-an-IIS-Application-Pool

Alternatives: Windows 2003 & II6 C:\WINDOWS\system32>cscript.exe iisapp.vbs /a AppPoolName /r

Windows 2008 IIS7 C:\WINDOWS\system32\inetsrv\appcmd recycle apppool “MyAppPool” Restart-WebAppPool cmdlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#    File: RecycleAppPool.ps1
#    Author: Ralph Willgoss
#    Date: 2nd Oct 2012
#    Reference:
#    http://stackoverflow.com/questions/198623/how-do-i-recycle-an-iis-apppool-with-powershell
# =============================================================================
#    Iniatialise
=============================================================================
param ( )

=============================================================================
#   Main
=============================================================================

Write-OutPut ""
Write-OutPut "Starting Recycling App Pool"
Write-OutPut ""

$appPoolName = "AppPoolName" #$args[0]
$appPool = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool"
          | Where-Object { $_.Name -eq "W3SVC/APPPOOLS/$appPoolName" }
          
$appPool.Recycle()

Write-OutPut ""
Write-OutPut "Finished Recycling App Pool"
Write-OutPut ""

Originally posted on http://geekswithblogs.net/rwillgoss/archive/2012/11/25/how-do-i-recycle-an-iis-app-pool-with-powershell.aspx