PowerShell Script to Create Web Application and Site Collection in SharePoint 2010
As we all know every time we want to create a web application in SharePoint we first have to create a new web application and then at the root of the application we have to create a site collection using SharePoint Site Collection templates.
If we just create a web application and try to access the site it will not show anything.
To combine the process I created this simple PowerShell script to do both at the same time.
——————————————–
# This script will create new Web Application and then create a new Site Collection
# Set Execution Policy
Set-ExecutionPolicy unrestricted
#Gather the web application and site collection variables
$Name = Read-Host(“Enter the name of Web Application”)
$DatabaseName = Read-Host(“Enter Database Name”)
$Port = Read-Host (“Enter the Port Number”)
$URL = Read-Host(“Enter the URL”)
$AppPoolName = Read-Host (“Enter the Application Pool Name”)
$AppPoolAcct = Read-Host (“Enter the Application Pool account Domain\user”)
$SiteName = Read-Host (“Enter the Name of Site Collection”)
$SiteURL = Read-Host (“Enter the Name of the Site Collection Url”)
$SiteOwner = Read-Host (“Enter the Site Collection Owner”)
$Template = Read-Host (“Enter the Site Template e.g. STS#0”)
# Create Web Application
function create_WebApp
{
New-SPWebApplication -Name $Name -Port $Port -URL $URL -ApplicationPool $AppPoolName -ApplicationPoolAccount $AppPoolAcct -DatabaseName $DatabaseName
}
# Create Site Collection
function create_SiteCollection
{
New-SPSite $SiteURL -OwnerAlias $SiteOwner -Template $Template
}
if (create_WebApp)
{
Write-Host $URL
create_SiteCollection
}
else
{
Write-Host “Error”
}
if (create_SiteCollection)
{
Write-Host $SiteUrL
}
——————————————————————-
Thanks