Home > SharePoint 2010, SharePoint Advancements > PowerShell Script: (Creating a Site Collection in a Managed Path In It’s Own Content Database)

PowerShell Script: (Creating a Site Collection in a Managed Path In It’s Own Content Database)

Did I mention I love PowerShell.  Well I’ll mention it again.  I love PowerShell.  It makes for performing things within SharePoint 2010 so much easier through automation.

Below is a basic script that will Create a New Site Collection in a Managed Path and in It’s own Content Database.  Just copy the script below into Notepad and save as .ps1.

The Script checks to see if a Managed Path already exists in the Web Application.  If not it creates it.  Creates the new Content Database, Creates the new Site Collection in the new Managed Path, Assigns Primary/Secondary Site Owners, Sets the Site Collection to Anonymous Access.

—————————————————————————————————————————————————

Write-Host “********************************************************************************************************”
Write-Host “*     Checking to see if Managed Path already Exists                                                                 *”
Write-Host “********************************************************************************************************”

$WebApplicationURL = Read-Host “Enter in Web Application URL”
$ManagedPathName = Read-Host “Enter in Managed Path”

$ManagedPath = Get-SpManagedPath -WebApplication $WebApplicationURL -Identity $ManagedPathName -ErrorAction SilentlyContinue
if ($ManagedPath -ne $null)
{
Write-Host “Managed Path $ManagedPathName already Exists”.
}
else
{
Write-Host “Managed Path Does Not Exist, Creating Managed Path $ManagedPathName ….”
New-SPManagedPath -RelativeURL $ManagedPathName -WebApplication $WebApplicationURL -Explicit
}

Write-Host “*********************************************************************************************************”
Write-Host “*     Creating New Content Database                                                                                             *”
Write-Host “*********************************************************************************************************”

$NewContentDB = Read-Host “Enter In Name of New Content Database”
New-SPContentDatabase -name $NewContentDB -WebApplication $WebApplicationURL
Write-Host “Content Database Created”

Write-Host “**************************************************************************************”
Write-Host “*      Creating New Site Collection                                                  *”
Write-Host “**************************************************************************************”

$Template = “STS#0”
$Domain = Read-Host “Enter In Domain”
$NewSiteCollectionURL = $WebApplicationURL + “/” + $ManagedPathName
$PrimaryOwnerAlias = Read-Host “Enter In Primary Site Collection Owner”
$SecondaryOwnerAlias = Read-Host “Enter In Secondary Site Collection Owner”
$OwnerAlias = $Domain + “\” + $PrimaryOwnerAlias
$SecondaryAlias = $Domain + “\” $SecondaryOwnerAlias

New-SPSite -Url $NewSiteCollectionURL -ContentDatabase $NewContentDb -OwnerAlias $OwnerAlias -SecondaryOwnerAlias $SecondaryAlias -Template $Template

$Web = Get-SPWeb $NewSiteCollectionURL
$Web.AnonymousState = 2;

Write-Host “Site Collection Created Succesfully…”
Write-Host “Site Collection Owners Added”
Write-Host “Anonymous Access Enabled”

—————————————————————————————————————————————————-

That’s it, pretty simple.  I will be modifying this script a little to display all the different SharePoint Templates you can choose from when creating your Site Collection.  Currently this script is using the Team Site template.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: