Changing Application Pool for existing Web Applications

There are many articles about SharePoint 2010 and application pool management. One of those topics is the number of application pools to be used by your SharePoint web applications.  I like this post by Todd Klindt: Top 10 SharePoint 2010 Configuration Mistakes. Mistake #5 is about application pools and web applications. There’s no need to have an application pool for each web application. Sharing an application pool by several web applications saves you lots of megabytes of memory.

With Central Administration it is undoable to set a different application pool for a web application. First, you have to delete the web application without deleting the IIS site and content databases. Then you can recreate the web application using the correct application pool, the existing IIS site and finally attaching the content databases. If you have features activated at the web application scope, you also have to reactivate them again and Lord knows what happens then!

You don’t want this. No, you don’t.

Alternative? PowerShell of course! Basically you will need a reference to the application pool first, then a reference to the web application and then you can update the ApplicationPool property.

At one of my customer’s SharePoint Farm I noticed that every web application had it’s own Application Pool. What a waste of resources that is. Time for scripting, I guess.

Here’s my PowerShell Script:

$config = Get-Content Set-DefaultApplicationPoolConfig.xml
$defaultAppPoolName = $config.WebApplications.defaultAppPoolName


$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService 

Write-Host "Checking presence default application pool $defaultAppPoolName..." -NoNewline

[Microsoft.SharePoint.Administration.SPApplicationPool] $newAppPool = $service.ApplicationPools[$defaultAppPoolName] 

## The Default Application Pool MUST exist!
if($newAppPool -eq $NULL) 
    Write-Host ""
    Write-Host -ForegroundColor Red "The default application pool '$defaultAppPoolName' does not exist." 
    Write-Host -ForegroundColor Red "Please ensure the application pool '$defaultAppPoolName' is already registered in SharePoint." 
    Write-Host -ForegroundColor Green "[OK]"

foreach( $webAppConfig in $config.WebApplications.WebApplication )
    $url = $webAppConfig.url
    Write-Host "Updating $url..." -NoNewline 

    $webApp = Get-SPWebApplication $url -ErrorAction SilentlyContinue
    if( $webApp -eq $null )
        Write-Host -ForegroundColor Yellow " [Web application does not exist]"

    $currentAppPool = $webApp.ApplicationPool
    if($currentAppPool.Name -eq $defaultAppPoolName) 
        Write-Host -ForegroundColor Green " [Change not needed]"
        $webApp.ApplicationPool = $newAppPool
        Write-Host -ForegroundColor Green " [Done]"

As you can see this script uses a XML configuration file. In this file I configure what the default application pool name is and which web applications I want to change to this application pool.

XML Configuration file:

<WebApplications defaultAppPoolName="SharePoint - Default Web Application Pool" >
    <WebApplication url="http://dummy.ochadev.local" />
    <WebApplication url="http://redactie.diwug.local" />
    <WebApplication url="http://playground.ochadev.local" />
    <WebApplication url="http://testdummy" />

And this is the result:


After executing the script the SharePoint web applications are registered with the new default application pool. This also means that whenever you extend your farm with a new SharePoint server, it also has the correct web applications and their application pools.

Good luck and enjoy.

Download script