Creating a site collection in a specific DB from Powershell

Requirement

Create a site collection in a specific / new database from Powershell without having to manually define a managed path in Central Administration.

Solution

You can do this with the following PowerShell Script.

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

$server = Read-Host “Enter SQL Server”

$dbname = Read-Host “Enter Database Name”

$webapp = Read-Host “Enter Web Application URL”

$site = Read-Host “Enter New Site Collection URL”

$owner1 = Read-Host “Enter Primary Site Collection Admin”

$owner2 = Read-Host “Enter Secondary Site Collection Admin”

New-SPContentDatabase -Name $dbname -DatabaseServer $server -WebApplication $webapp | out-null

# Uncomment the following line and change the substring(32) to the number of characters in your Web Application URL.

# E.g. If your site collection is http://www.sharepoint.com/site, then change ’32’ to ’25’ to that it picks up /site as your managed path.

# new-spmanagedpath -RelativeURL $site.substring(32) -WebApplication $webapp –Explicit

# Otherwise, you must create your managed path manually through Central Administration or Powershell.

New-SPSite -URL $site -OwnerAlias $owner1 -SecondaryOwnerAlias $owner2 -ContentDatabase $dbname | out-null

Get-SPContentDatabase -Site $site | Set-SPContentDatabase -MaxSiteCount 1 -WarningSiteCount 0

Write-Host ” ”

Write-Host “Site Collection at” $site “has been created in the” $dbname “content database” -ForegroundColor Yellow

 

This script will:

  1. Create a managed path based on the name of the site URL, e.g. http://www.sharepoint.com/site will create a managed path called /site.
  2. Create a site collection in the specified database on the specified database server.

 

 

Tags:

No comments yet.

Leave a Reply

%d bloggers like this: