Jaa


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 (https://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:  https://technet.microsoft.com/en-us/library/ff607831(v=office.15).aspx

asnp *SharePoint* -ErrorAction SilentlyContinue
$WebAppURL = "https://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

  • Anonymous
    January 01, 2003
    thank you
  • Anonymous
    January 01, 2003
    awesome, thank you.
  • Anonymous
    January 01, 2003
    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.
  • Anonymous
    July 26, 2014
    This only works on single server farms.
  • Anonymous
    August 08, 2014
    Works for me fine in a 2 server farm.
  • Anonymous
    March 23, 2015
    It is incredible and informative knowledge. Impressive.
    http://staygreenacademy.com">SharePoint 2013 Videos
  • Anonymous
    May 07, 2015
    Unnecessary use of New-Object, since there is a cmdlet: New-SPServiceApplicationPool
  • Anonymous
    October 16, 2015
    Will this code push the new application pool to existing application servers running from same WFE?
  • Anonymous
    October 27, 2015
    @Minu - Yes
  • Anonymous
    March 23, 2016
    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?