21

I want to recycle my application pool using one-liner command which I can put in my PowerShell script. I added the following code in my PowerShell script:

Import-Module WebAdministration $site = "Default Web Site" $pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool Restart-WebAppPool $pool 

But I am getting an error that name IIS doesn't exist. How can I fix it?

3
  • 1
    Please ignore my above comment as I am unable to edit it. Following is what I want to say: I added the following code in my powershell script: Restart-WebItem 'IIS:\AppPools\DefaultAppPool' Import-Module WebAdministration $site = "Default Web Site" $pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool Restart-WebAppPool $pool But I get error that name IIS doesn't exist. I am unable to figure it out Commented Jul 11, 2016 at 18:12
  • 1
    I'd recommend editing your question, rather than adding comments. That's generally how folks do it here at StackOverflow. Check out the "How to ask" article, too: stackoverflow.com/help/how-to-ask Commented Jul 11, 2016 at 18:19
  • 1
    Thanks for the advise Trevor. Commented Jul 11, 2016 at 19:27

9 Answers 9

33

Short and simple, like this...

Restart-WebAppPool (Get-Website -Name <YourSiteName>).applicationPool 
Sign up to request clarification or add additional context in comments.

3 Comments

Does this recycle or restart? I believe those are two different things
For application pool this is the same. New process will be created and new memory allocated. Where and why you should restart app pool, and when it must be recycling?)
Description on msdn says - this command causes the specified application pool to be recycled.
14

You can use appcmd.exe:

appcmd recycle apppool /apppool.name:'MyAppPool' 

You can also retrieve the corresponding WMI instance and invoke the Recycle() method:

$myAppPool = Get-WmiObject -Namespace root\WebAdministration -Class ApplicationPool -Filter "Name = 'MyAppPool'" $myAppPool.Recycle() 

2 Comments

Thanks for your response. But I want to achieve it through powershell script because I am performing other tasks too along with recycling.
The syntax is incorrect - it should be /apppool.name:'MyAppPool' with a colon, not equals.
4

This may work:

Write-Host "App Pool Recycling Started...." & $env:windir\system32\inetsrv\appcmd list apppools /state:Started /xml | & $env:windir\system32\inetsrv\appcmd recycle apppools /in Write-Host "App Pool Recycling Completed" 

It works for me in AWS through the Run command.

Comments

3

The following command works for me

invoke-command -computername servername -scriptblock {C:\Windows\System32\inetsrv\appcmd.exe recycle apppool "apppoolname"} 

Comments

3

Use:

Import-Module WebAdministration $site = "MySite" $pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool #Recycle the application pool: Restart-WebAppPool $pool 

3 Comments

please add some explanation about your proposed solution. You can read how to write good answers
An explanation would be in order. E.g., what is the idea/gist? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
OK, the OP seems to have left the building ("Last seen more than 2 years ago"). Perhaps somebody else can reverse engineer it and provide some information (preferably by editing, not here comments) - but ********* without ********* any kind of meta information like "Edit:", "Update:", or similar - the answer should appear as if it was written today. Use the edit summary (or comments) to describe and justify the change.
3

Since WebAdministration is old and badly supported under PowerShell 7, here's an updated answer using the fresher IISAdministration module:

Import-Module IISAdministration $pool = Get-IISAppPool -Name "DefaultAppPool" $pool.Recycle() 

You can also do this as a two-liner, or even one-liner if you import the module in your profile:

Import-Module IISAdministration (Get-IISAppPool("DefaultAppPool")).Recycle() 

Do note that accessing the IISServerManager object requires administrative permissions.

Comments

1

I don't run all PowerShell scripts as administrator - it's dangerous.

But appcmd.exe is not available unless running in an administrator prompt.

This command line will prompt for elevation if you're running in a regular, un-elevated prompt:

Start-Process c:\windows\System32\inetsrv\appcmd.exe "recycle apppool /apppool.name:MyAppPool" -Verb RunAs 

Comments

1

In case you need to recycle all the Application Pools:

 Import-Module IISAdministration Get-IISAppPool | ForEach-Object{ if($_.State -eq "Started"){ $_.Recycle() } } 

If the AppPool is stopped it will throw an exception

Comments

1

Use the -Name option:

IIS:\> Restart-WebAppPool -Name "DefaultAppPool" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.