Power Shell for Creating Site Collection

The following code allows to create a site collection with a dedicated content DB. it also sets a Managed Path if needed and the site owners.

# Create a site collection with Dedicated Content DB in the 2013 departments.essendant.com

# Farm Account with DB access is needed to run this code !!!

clear
Set-ExecutionPolicy Unrestricted
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
        Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell
}

$siteName = “[Your Site Name]”;
$WebsiteDesc = “[Description]”;
$webAppUrl = [Your Site Url]

$siteUrl = $webAppUrl +”/sites/”+ $siteName; # for sites pre defined managed path and you will also need to comment out the New-SPManagedPath

OR

$siteUrl = $webAppUrl $siteName;  # use this option when you create managed path sites.

$Template = “BLANKINTERNET#0#- publishing site #”STS#0″ #Team Site STS#1 – BlankSite
$ownerAlias = “Domain\[your site owner 1 account]”;
$secondaryOwnerAlias = “Domain\[your site owner 2 account]”;
$databaseName = “WSS_Content_$siteName”;
$databaseServer = “[your server name]”;
$OutputFile = “C:\Sharepoint\PowerShellScripts\NewSitesCollections\SiteCollection_$siteName.txt”

“New Site Collection” >> $OutputFile

Write-Output $siteUrl >> $OutputFile

# create a managed path

New-SPManagedPath –RelativeURL $siteName -WebApplication $webAppUrl -Explicit | tee $OutputFile

#Create new content database for DMS department site collection

New-SPContentDatabase -Name $databaseName -DatabaseServer $databaseServer -WebApplication $webAppUrl >> $OutputFile

#Create new site collection

New-SPSite -Url $siteUrl -OwnerAlias $ownerAlias -SecondaryOwnerAlias $secondaryOwnerAlias -ContentDatabase $databaseName -Template $template -Name $siteName >> $OutputFile

#Add the sharepont support group to the site collection adminsistrators

New-SPUser -UserAlias ‘NA\MyName’ -DisplayName ‘Sharepoint SUpport’ -Web $siteUrl -SiteCOllectionAdmin >> $OutputFile

 

 

Short version :

clear
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell
}
$SiteName = “SiteName”;
#Create Content Database
New-SPContentDatabase -name “WSS_Content_$($SiteName)” -WebApplication “http://sharepoint2/”
 #Create Managed Path
New-SPManagedPath -RelativeURL $SiteName -WebApplication “http://sharepoint2/” -Explicit
 # Create Site Collection
New-SPSite “http://sharepoint2/$($SiteName)” -OwnerAlias NA\UserId -Name $SiteName -Template “STS#0” -ContentDatabase “WSS_Content_$($SiteName)”