Here is a simple script for creating a new Web Application in SharePoint. This will be the first out of a series of post on the topic were we will go through creating, extending and finally give you a script to automate the creation and extending multiple Web Applications with root site collections. Very useful in disaster and recovery scenarios or when you have a lot of Web Apps that you need to create in the same way on different staging environments.

So, start of with the usual connection to the farm:

[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$farm = [microsoft.sharepoint.administration.spfarm]::local

We then create a new SPWebApplicationBuilder object and add the settings we want. Note that you need to change the values below to your own values. The values are quite self-explanatory so I will not go through all of them.

$WebAppBuilder = new-object microsoft.sharepoint.administration.SPWebApplicationBuilder($farm)

$WebAppBuilder.ApplicationPoolId = “SharePoint_TestFarm_WebApp”

$WebAppBuilder.ApplicationPoolUsername = “Domain\AppPoolUserNameUsername”
$WebAppBuilder.ApplicationPoolPassword = “AppPoolPassword”
$WebAppBuilder.Port = 8080
$WebAppBuilder.ServerComment = “New WebApp”
$WebAppBuilder.CreateNewDatabase = $true
$WebAppBuilder.DatabaseServer = “DEVWSS\Microsoft##SSEE”
$WebAppBuilder.DatabaseName = “TestFarm_ContentDB_01″
$WebAppBuilder.RootDirectory = “C:\Inetpub\wwwroot\wss\VirtualDirectories\NewWebApp”
$WebAppBuilder.UseSecureSocketsLayer = $false
$WebAppBuilder.AllowAnonymousAccess = $false
$webapp = $WebAppBuilder.Create()
$webapp.Provision()

In the script above I use the ApplicationPoolId. If you type a name of a application pool that does not exist it will be created and you will need the ApplicationPoolUsername and ApplicationPoolPassword. If you want to share Application Pool you skip the username and password and type the existing Application Pool Name.
We then end with the Create() and the Provision().

If you have a farm with more then one Web Front End Server you will need to add the below lines to make sure that the ApplicationPoolUsername and Password is deployed to the other WFEs. In that case, skip the ApplicationPool Username and Password above. You will also need to replace the Provision() with ProvisionGlobally.

$webapp.ApplicationPool.Username = “Domain\AppPoolUserNameUsername”
$webapp.ApplicationPool.Password = “AppPoolPassword”

$webapp.ApplicationPool.Update()
$webapp.ApplicationPool.Deploy()

That’s about it. In part two we will look into how we can extend our new Web Application.



  1. [...] two earlier posts I have written about how to create and extend a web application using PowerShell. Today I ran in to a problem when I extended a Web [...]

  2. [...] two earlier posts I have written about how to create a Web Application and how to extend a Web Applicationusing PowerShell. Now it’s time to combine the two into one [...]

  3. greg williams on Tuesday 23, 2009

    is there a something simular to construct a site collection?

  4. Mattias Karlsson on Tuesday 23, 2009

    Hi Greg,

    I recommend you to look at my web application builder script found here: http://mysharepointofview.com/2009/12/updated-version-of-the-create-web-application-script/

    The short version is:
    $WebAppURL = “http://webapplicatoinurl”
    $Path = “/”
    $Title = “Site Title”
    $Description = “Site Description”
    $AdminAccount = “domain\primarysitecolladmin”
    $AdminName = “Admin name”
    $AdminEmail = “Admin email”
    $Template = “and site template like: STS#1″
    $webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)

    $SiteCollection = $webApp.Sites.Add( $Path, $Title, $Description, 1033, $Template, $AdminAccount, $AdminName, $AdminEmail)

    Hope this could help



Subscribe without commenting