The web is littered with different ways to restart IIS application pools. Unfortunately, no one method that I’ve found works consistently across both IIS 6 and II7. In this simple snippet, you can see the simple function I crafted that works across both current versions of IIS.
function Restart-IISAppPool {
param(
$PoolName
)
if (Test-Path c:\Windows\system32\inetsrv\appcmd.exe) {
Write-Host "Restarting AppPool on IIS7..."
c:\windows\system32\inetsrv\appcmd stop apppool /apppool.name:$PoolName
c:\windows\system32\inetsrv\appcmd start apppool /apppool.name:$PoolName
}
else {
Write-Host "Restarting AppPool on IIS6..."
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$PoolName"}
$appPool.Recycle()
}
}
You can use the function like so:
Restart-IISAppPool "MyAppPool"
Enjoy!
Tags: