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