How do I recycle an IIS AppPool with Powershell? How do I recycle an IIS AppPool with Powershell? powershell powershell

How do I recycle an IIS AppPool with Powershell?


Where-Object is a filter that expects something as in input. There seems to be a missing pipe, before the where filter.

Try:

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

Edit: I noticed that the WMI class was IISApplicationPools, which as you saw, did not show us the Recycle method when piped to Get-Member. This needs to be changed to IISApplicationPool (non-plural). With that change, you are able to use the Recycle method. The code above has been updated.


Using the data from this question I was able to create 2 very useful functions.

  • Get-IisAppPools
  • Recycle-IisAppPool

The code:

function Get-IisAppPools {    Get-WmiObject -Namespace "root\MicrosoftIISv2" -Class "IIsApplicationPool" -Filter 'name like "W3SVC/APPPOOLS/%"'          | ForEach-Object { $_.Name.ToString().SubString(15) } }function Recycle-IisAppPool([string]$appPoolName) {      Invoke-WmiMethod -Name Recycle -Namespace "root\MicrosoftIISv2" -Path "IIsApplicationPool.Name='W3SVC/APPPOOLS/$appPoolName'" }

You can use these functions like this

Recycle-IisAppPool DefaultAppPoolGet-IisAppPools | ? { $_ -match "v4.0$" } | % { Recycle-IisAppPool $_ }


When using get-WMIObject you should probably use -filter instead of piping to Where-Object. the filter parameter uses WQL syntax language instead of PowerShell's, so don't let that trip you up.

$appPoolName = $args[0]$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPools" -filter 'name="W3SVC/APPPOOLS/$appPoolName"'

Having said that putting the pipe there should work, and certainly makes it easier to work with unless you already know WQL.