Create Site Collection PowerShell Script SharePoint 2013 or 2010

InfoPath alternatives for form designing SharePoint

In this post we will discuss how to create site collection using PowerShell script in SharePoint 2013 or 2010.

Also check out:
– Office 365 Easy Form Development FORM7

– Office 365 SharePoint Easy Migration Part 2

– How to create event receivers using Visual Studio 2010 in SharePoint 2010?

Create Site collection provided by below inputs in CSV files.
SiteCollectionName
SiteCollectionDescription
SiteCollectionURL
SiteCollectionTemplate
SiteCollectionLanguage
SiteCollectionDatabaseName
PrimaryOwner
SecondaryOwner
WebApplicationURL

Please find the attached CSV files which will be used in the PS file.

PS code-

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$path = $(“$dp0\CreateSiteCollectionsWithContentDB.csv”)
$csv = Import-csv -path $path

foreach($line in $csv)
{
$WebApplicationURL = $line.WebApplicationURL
$SiteCollectionName = $line.SiteCollectionName
$SiteCollectionURL = $line.SiteCollectionURL
$SiteCollectionTemplate = $line.SiteCollectionTemplate
$SiteCollectionLanguage = $line.SiteCollectionLanguage
$SiteCollectionDatabaseName = $line.SiteCollectionDatabaseName
$PrimaryOwner = $line.PrimaryOwner
$SecondaryOwner = $line.SecondaryOwner
$SiteCollectionDescription = $line.SiteCollectionDescription

# Create a new Content Database for Sharepoint Site Collection

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) | Out-null
# Get Content db from Database instance
$ContentDB = Get-SPContentDatabase $SiteCollectionDatabaseName -ErrorAction SilentlyContinue
# if content db already exists
if($ContentDB.Name -ne $null)
{
Write-Host -ForegroundColor Red “Content Database: $SiteCollectionDatabaseName already Exists”.
}
else
{
Write-Host -ForegroundColor Yellow “Content Database Does Not Exist, Creating Content Database $SiteCollectionDatabaseName ….”
$ContentDataBase=New-SPContentDatabase -name $SiteCollectionDatabaseName -WebApplication $WebApplicationURL
Write-Host -ForegroundColor Green “Content DataBase: $ContentDataBase created successfully”

# Create a new Sharepoint Site Collection

$Site=Get-SPWeb $SiteCollectionURL -ErrorAction SilentlyContinue

# check whether site collection already exists
if($Site -ne $null)
{
Write-Host “Site Collection: $Site already Exists”.
}
else
{
Write-Host -ForegroundColor Yellow “Site Collection Does Not Exist, Creating Site Collection $Site ….”
$SiteCollection=New-SPSite -URL $SiteCollectionURL -ContentDataBase $SiteCollectionDatabaseName -OwnerAlias $PrimaryOwner -SecondaryOwnerAlias $SecondaryOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName -Description $SiteCollectionDescription
Write-Host -ForegroundColor Green “Site Collection: $SiteCollection created successfully”
}
}
}

Thanks,
Sambita

Similar SharePoint 2013 Tutorials


About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →

Leave a Reply