Create a SharePoint Application Pool using PowerShell


As you may know there isn't an out of the box PowerShell cmdlet available in SharePoint 2010 or 2013 that can be used to create an Application Pool for a Web Application, New-SPServiceApplicationPool (http://technet.microsoft.com/en-us/library/ff607595(v=office.15).aspx) is available however this can only be used to create an Application Pool for a Service Application.

A customer recently asked me to help them to re-configure one of their Web Applications to use a new Application Pool, I put this script together to facilitate this. The script performs the following actions:

  • Creates a new Application Pool
  • Associates the new Application Pool with an existing Web Application

Three variables need to be updated prior to running the script:

$WebAppURL is the URL of the Web App to change the Application Pool of.
$NewAppPoolName is the name of the name Application Pool that will be created.
$NewAppPoolUserName is the user account that the Application Pool will run under the context of.

The script will prompt for the credentials of the account specified in $NewAppPoolUserName. This account should be registered as a Managed Account in SharePoint prior to executing the script – New-SPManagedAccount: http://technet.microsoft.com/en-us/library/ff607831(v=office.15).aspx

asnp *SharePoint* -ErrorAction SilentlyContinue
$WebAppURL = "http://WebApp"
$NewAppPoolName = "NewAppPool"
$NewAppPoolUserName = "contoso\apppool"

$Farm = Get-SPFarm
$Service = $Farm.Services | where {$_.TypeName -eq "Microsoft SharePoint Foundation Web Application"}
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$NewAppPool = New-Object Microsoft.SharePoint.Administration.SPApplicationPool($NewAppPoolName,$Service)
$NewAppPool.CurrentIdentityType = "SpecificUser"
$NewAppPool.Username = $NewAppPoolUserName
$NewAppPool.SetPassword($Password)
$NewAppPool.Provision()
$NewAppPool.Update($true)

$NewAppPool = $Service.ApplicationPools[$NewAppPoolName]
$WebApp = Get-SPWebApplication $WebAppURL
$WAAppPool = $WebApp.ApplicationPool = $NewAppPool
$WebApp.Update()
$WebApp.ProvisionGlobally()


Brendan Griffin – @brendankarl

Comments (10)

  1. Anonymous says:

    thank you

  2. Anonymous says:

    awesome, thank you.

  3. Anonymous says:

    New-SPServiceApplicationPool creates an Application Pool for a Service Application – this cannot be used to create an Application Pool for a Web Application, which is why I wrote this Blog post. There isn’t a cmdlet available for creating an Application
    Pool for a Web Application.

  4. ryan says:

    This only works on single server farms.

  5. Works for me fine in a 2 server farm.

  6. SharePoint 2013 Videos says:

    It is incredible and informative knowledge. Impressive.
    http://staygreenacademy.com">SharePoint 2013 Videos

  7. Mel says:

    Unnecessary use of New-Object, since there is a cmdlet: New-SPServiceApplicationPool

  8. Minu says:

    Will this code push the new application pool to existing application servers running from same WFE?

  9. Simao Williams says:

    Great post thanks – I used something similar (but I prefer the way you register credentials to mine – much neater!) and added some checks for whether the app pool already included either in SharePoint or in IIS only (in the case of IIS only using a Test-Path
    on IIS:AppPoolsMyAppPool (with the WebAdministration module loaded to support IIS-style path evaluation.

    Interestingly, this second check (IIS only) was needed in my case because if the provisioning crashed mid-way for any reason (which it did a couple of times with a server problem), the site was sometimes created in IIS but not registered in the SP database.
    Have you experienced anything similar?