This script will create claims-based web applications in SharePoint 2013.  This script may also work in SharePoint 2010.  This script will also set your object cache user accounts, as well as configuring your object cache accounts for your web applications.  Significant conflict detection is included in this script, which should make creating web applications very simple.

 

More details regarding this script can be found in my recent blog post:
Creating Claims-Based Web Applications in SharePoint 2013

 

Download the script for a fully commented version

 

 

PowerShell
Edit|Remove
$ver = $host | select version 
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
{ 
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
Add-PSSnapin Microsoft.SharePoint.PowerShell 
} 
Write-Progress -Activity "Creating Web Application" -Status "Setting Variables" 
$WebApplicationURL = "http://Contoso.com" 
$WebApplicationName = "Contoso SharePoint Site" 
$ContentDatabase = "Contoso_ContentDB" 
$ApplicationPoolDisplayName = "SharePoint App Pool" 
$ApplicationPoolIdentity = "Contoso\SPAppPool" 
$ApplicationPoolPassword = "Pass@word1" 
$PortalSuperReader = "i:0#.w|Contoso\SuperReader" 
$PortalSuperUser = "i:0#.w|Contoso\SuperUser" 
Write-Progress -Activity "Creating Web Application" -Status "Loading Functions" 
Function CreateClaimsWebApp($WebApplicationName$WebApplicationURL$ContentDatabase$HTTPPort) 
{ 
    if($AppPoolUsed -eq $True) 
    { 
        Write-Progress -Activity "Creating Web Application" -Status "Using Application Pool With Existing Web Applications" 
        Set-Variable -Name WebApp -Value (New-SPWebApplication -ApplicationPool $ApplicationPoolDisplayName -Name $WebApplicationName -url $WebApplicationURL -port $HTTPPort -DatabaseName $ContentDatabase -HostHeader $hostHeader -AuthenticationProvider (New-SPAuthenticationProvider)) -Scope Script 
        Write-Progress -Activity "Creating Web Application" -Status "Configuring Object Cache Accounts" 
        SetObjectCache 
    } 
    else 
    { 
        Write-Progress -Activity "Creating Web Application" -Status "Using Application Pool With No Existing Web Applications" 
        Set-Variable -Name WebApp -Value (New-SPWebApplication -ApplicationPool $ApplicationPoolDisplayName -ApplicationPoolAccount $AppPoolManagedAccount.Username -Name $WebApplicationName -url $WebApplicationURL -port $HTTPPort -DatabaseName $ContentDatabase -HostHeader $hostHeader -AuthenticationProvider (New-SPAuthenticationProvider)) -Scope Script 
        Write-Progress -Activity "Creating Web Application" -Status "Configuring Object Cache Accounts" 
        SetObjectCache 
    } 
} 
Function ValidateURL($WebApplicationURL) 
{ 
    if(get-spwebapplication $WebApplicationURL -ErrorAction SilentlyContinue) 
    { 
        Write-Progress -Activity "Creating Web Application" -Status "Aborting Process Due To URL Conflict" 
        Write-Host "Aborting: Web Application $WebApplicationURL Already Exists" -ForegroundColor Red 
        sleep 5 
        Set-Variable -Name CriticalError -Value $True 
    } 
    elseif($WebApplicationURL.StartsWith("http://")) 
        { 
            Set-Variable HostHeader -Value ($WebApplicationURL.Substring(7)) -Scope Script 
            Set-Variable -Name HTTPPort -Value "80" -Scope Script 
        } 
        elseif($WebApplicationURL.StartsWith("https://")) 
        { 
            Set-Variable HostHeader -Value ($WebApplicationURL.Substring(8)) -Scope Script 
            Set-Variable -Name HTTPPort -Value "443" -Scope Script 
        } 
} 
Function ValidateAppPool($AppPoolName$WebApplicationURL) 
{ 
    $CurrentErrorActionPreference = $ErrorActionPreference 
    $ErrorActionPreference = "SilentlyContinue" 
    $TestAppPool = Get-WebAppPoolState $AppPoolName 
    if(Get-SPServiceApplicationPool $AppPoolName) 
    { 
        $AppPools = Get-SPWebApplication | select ApplicationPool 
        if($AppPools) 
        { 
            foreach($Pool in $AppPools) 
            { 
                [Array]$Poolchild = $Poolchild += ($Pool.ApplicationPool.DisplayName) 
                if($Poolchild.Contains($ApplicationPoolDisplayName)) 
                { 
                    Set-Variable -Name AppPoolUsed -Value $True -Scope Script 
                } 
                else 
                { 
                    Set-Variable -Name AppPoolUsed -Value $False -Scope Script 
                } 
            } 
        } 
        Set-Variable -Name AppPool -Value (Get-SPServiceApplicationPool $AppPoolName-scope Script 
        Set-Variable -Name AppPoolManagedAccount -Value (Get-SPManagedAccount | ? {$_.username -eq ($AppPool.ProcessAccountName)}) -scope Script 
    } 
    elseif($TestAppPool) 
    { 
        Write-Host "Aborting: Application Pool $AppPoolName already exists on the server and is not a SharePoint Application Pool `n`rWeb Application `"$WebApplicationURL`" will not be created" -ForegroundColor Red 
        Set-Variable -Name CriticalError -Value $True 
    } 
    elseif(!($TestAppPool)) 
    { 
        validateManagedAccount $ApplicationPoolIdentity 
        if($ManagedAccountExists -eq $True) 
        { 
            Write-Host "Creating New App Pool using Existing Managed Account" 
            Set-Variable -Name AppPoolManagedAccount -Value (Get-SPManagedAccount $ApplicationPoolIdentity | select username) -scope "Script" 
            Set-Variable -Name AppPool -Value (New-SPServiceApplicationPool -Name $ApplicationPoolDisplayName -Account $ApplicationPoolIdentity-scope "Script" 
        } 
        else 
        { 
            Write-Host "Creating New Managed Account And App Pool" 
            $AppPoolCredentials = New-Object System.Management.Automation.PSCredential $ApplicationPoolIdentity, (ConvertTo-SecureString $ApplicationPoolPassword -AsPlainText -Force) 
            Set-Variable -Name AppPoolManagedAccount -Value (New-SPManagedAccount -Credential $AppPoolCredentials-scope "Script" 
            Set-Variable -Name AppPool -Value (New-SPServiceApplicationPool -Name $ApplicationPoolDisplayName -Account (get-spmanagedaccount $ApplicationPoolIdentity)) -scope "Script" 
        } 
    } 
    $ErrorActionPreference = $CurrentErrorActionPreference 
} 
Function ValidateManagedAccount($ApplicationPoolIdentity) 
{ 
    if(Get-SPManagedAccount $ApplicationPoolIdentity -ErrorAction SilentlyContinue) 
    { 
        Set-Variable -Name ManagedAccountExists -Value $True -Scope Script 
    } 
    else 
    { 
        Set-Variable -Name ManagedAccountExists -Value $False -Scope Script 
    } 
} 
 
Function ClearScriptVariables 
{ 
    $CurrentErrorActionPreference = $ErrorActionPreference 
    $ErrorActionPreference = "SilentlyContinue" 
    Remove-Variable $CriticalError -ErrorAction SilentlyContinue 
    $ErrorActionPreference = $CurrentErrorActionPreference 
} 
Function SetObjectCache 
{ 
    $WebApp.Properties["portalsuperuseraccount"] = $PortalSuperUser 
    $WebApp.Properties["portalsuperreaderaccount"] = $PortalSuperReader 
    $SuperUserPolicy = $WebApp.Policies.Add($PortalSuperUser"Portal Super User Account") 
    $SuperUserPolicy.PolicyRoleBindings.Add($WebApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)) 
    $SuperReaderPolicy = $WebApp.Policies.Add($PortalSuperReader"Portal Super Reader Account") 
    $SuperReaderPolicy.PolicyRoleBindings.Add($WebApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead)) 
    $WebApp.Update() 
} 
ClearScriptVariables 
Write-Progress -Activity "Creating Web Application" -Status "Validating Web Application URL Variables" 
ValidateURL $WebApplicationURL 
Write-Progress -Activity "Creating Web Application" -Status "Validating Application Pool Variables" 
ValidateAppPool $ApplicationPoolDisplayName $WebApplicationURL 
if(!($CriticalError)) 
{ 
Write-Progress -Activity "Creating Web Application" -Status "Creating Claims-Based Web Application" 
CreateClaimsWebApp $WebApplicationName $WebApplicationURL $ContentDatabase $HTTPPort 
}