Create Site Collection PowerShell Script SharePoint 2013 or 2010


This SharePoint 2013  how to create a site collection using PowerShell script in SharePoint 2013 or 2010. So here we will read the site details from a CSV file and then we will create a site collection using PowerShell

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. The PowerShell script, you can write, test and debug using Windows PowerShell ISE or by using Visual Studio code.




PowerShell 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”
}
}
}

Read some SharePoint PowerShell tutorials:


Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online 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 →