Creating SharePoint 2010 Site Collections with a Powershell Script

I’ve created the following PowersShell 2.0 script for creating Site Collections in a SharePoint 2010 environment. You can define the following variables:

  • Create a new Managed path (Explicit or not)
  • Create a new Content Database or use a existing one
  • Associated groups will be created
  • First & Second Administrator
  • Template
  • Name, Description Site Collection

My next step with this script is the possibility to read all the variables from a Excel sheet. That will give you the possibility to create numerous Site Collections from one Excel sheet.

# <# # .DESCRIPTION #		This script is for creating a Site Collection within a Web application, #		with owner, member and reader group and users. # .NOTES #		File Name	: Create_SiteCollection_Groups.ps1 #		Author		: Ronald Bruinsma @ iDocs #		Requires	: PowerShell Version 2.0 #		Date		: 30 August 2010 # # .LINK #		URL		: http://www.idocs.info # #>
# ##
# # Start of script
# ##

# ---- CONFIGURATION BLOCK ----

#Include the SharePoint cmdlets:
#Add-PsSnapin Microsoft.SharePoint.PowerShell

#user input and set farm variables
	Write-Host 'Create SharePoint 2010 Site Collection with permissions and groups'
	Write-Host '----------------------------------------------------------'
	Write-Host 'Site Collection information:'
	Write-Host '----------------------------------------------------------'

	#Managed path needed or not?
	$strContinue_managedPath = read-host -prompt 'Create a new Managed Path? (Y/N)'
	if ($strContinue_managedPath -eq 'Y'){
		$sp_siteCollectionManagedPath = Read-Host 'Managed Path name <managed_path_name>?'
		$sp_webapp_name_URL = Read-Host -Prompt 'Web application name <http://webapp_name>?'

		$strContinue_managedPath_Explicit = read-host -prompt 'Explicit inclusion? (Y/N)'
			if ($strContinue_managedPath_Explicit -eq 'Y'){
		 		Write-Host '----------------------------------------------------------'
				Write-Host 'Created this managed path:'
				New-SPManagedPath -RelativeURL $sp_siteCollectionManagedPath -WebApplication $sp_webapp_name_URL -Explicit
				$strContinue_managedPath_Explicit = 'Y'
				}
			else
			{
				Write-Host '----------------------------------------------------------'
				Write-Host 'Created this managed path:'
				New-SPManagedPath -RelativeURL $sp_siteCollectionManagedPath -WebApplication $sp_webapp_name_URL
			}
		Write-Host '----------------------------------------------------------'
		Write-Host 'Web application + managed path used for Site Collection:'
		$sp_webapp_name = $sp_webapp_name_URL + '/' + $sp_siteCollectionManagedPath + '/'
		Write-Host $sp_webapp_name
		Write-Host '----------------------------------------------------------'
	}
	else
	{
		#No Managed path needed, continue the script:
		Write-Host '----------------------------------------------------------'
		$sp_webapp_name_URL = Read-Host -Prompt 'Web application name <http://webapp_name>?'
		Write-Host '----------------------------------------------------------'
		Write-Host 'Available Managed Paths:'
		$sp_available_mangedPaths = Get-SPManagedPath -WebApplication $sp_webapp_name_URL
		foreach ($path in $sp_available_mangedPaths)
		{
			$path
		}
		Write-Host '----------------------------------------------------------'
		$sp_siteCollectionManagedPath = Read-Host -Prompt 'Managed Path to use?'
		$sp_webapp_name = $sp_webapp_name_URL + '/' + $sp_siteCollectionManagedPath + '/'
	}

	#New Content Database needed or not?
	$strContinue_contentDatabase = read-host -prompt 'Create a new Content Database? (Y/N)'
	if ($strContinue_contentDatabase -eq 'Y'){
		$sp_contentdatabase_name = Read-Host -Prompt 'Name for new Content Database?'
		Write-Host '----------------------------------------------------------'
		Write-Host 'Created this Content Database:'
		New-SPContentDatabase -Name $sp_contentdatabase_name -WebApplication $sp_webapp_name_URL
	}
	else
	{
		#No Content Database needed, continue the script:
		Write-Host '----------------------------------------------------------'
		Write-Host 'Available Content Databases:'
		Write-Host '----------------------------------------------------------'
		$sp_available_contentDatabases = Get-SPContentDatabase -WebApplication $sp_webapp_name_URL
		foreach ($db in $sp_available_contentDatabases)
		{
			$db.Name
		}
		Write-Host '----------------------------------------------------------'
		$sp_contentdatabase_name = Read-Host -Prompt 'ContentDatabase to use for this Site Collection?'
	}

	$sp_siteCollection_name = Read-Host -Prompt 'Site Collection name (if Managed Path is explicit than leave empty)?'
	$sp_siteCollection_description = Read-Host -Prompt 'Site Collection description?'
	Write-Host '----------------------------------------------------------'
	Write-Host 'Template CODES'
	Write-Host '----------------------------------------------------------'
	Write-Host ' Name                 Title                                    LocaleId   Custom'
	Write-Host ' —-------             ----—–----—–                             ---—–      ---——'
	Write-Host ' GLOBAL#0             Global template                          1033       False'
	Write-Host ' STS#0                Team Site                                1033       False'
	Write-Host ' STS#1                Blank Site                               1033       False'
	Write-Host ' STS#2                Document Workspace                       1033       False'
	Write-Host ' MPS#0                Basic Meeting Workspace                  1033       False'
	Write-Host ' MPS#1                Blank Meeting Workspace                  1033       False'
	Write-Host ' MPS#2                Decision Meeting Workspace               1033       False'
	Write-Host ' MPS#3                Social Meeting Workspace                 1033       False'
	Write-Host ' MPS#4                Multipage Meeting Workspace              1033       False'
	Write-Host ' CENTRALADMIN#0       Central Admin Site                       1033       False'
	Write-Host ' WIKI#0               Wiki Site                                1033       False'
	Write-Host ' BLOG#0               Blog                                     1033       False'
	Write-Host ' SGS#0                Group Work Site                          1033       False'
	Write-Host ' TENANTADMIN#0        Tenant Admin Site                        1033       False'
	$sp_siteCollection_template_code = Read-Host -Prompt 'Site Collection template code?'
	Write-Host '----------------------------------------------------------'
	Write-Host 'Site Collection Primary Adminitrator information:'
	Write-Host '----------------------------------------------------------'
	$sp_website_primaryLogin_name = Read-Host -Prompt 'Primary Administrator Login name (?'
	$sp_website_primaryDisplay_name = Read-Host -Prompt 'Primary Administrator Display name?'
	$sp_website_primaryEmail_name = Read-Host -Prompt 'Primay Administrator Email address?'
	Write-Host '----------------------------------------------------------'
	Write-Host 'Site Collection Secondary Adminitrator information:'
	Write-Host '----------------------------------------------------------'
	$sp_website_secondaryLogin_name = Read-Host -Prompt 'Secondary Administrator Login name (?'
	$sp_website_secondaryDisplay_name = Read-Host -Prompt 'Secondary Administrator Display name?'
	$sp_website_secondaryEmail_name = Read-Host -Prompt 'Secondary Administrator Email address?'
 	Write-Host '----------------------------------------------------------'
	Write-Host 'Building Site Collection and associating groups and Administrators'
	Write-Host '----------------------------------------------------------'

$sp_siteCollection_URL = $sp_webapp_name + $sp_siteCollection_name
$sp_website_membersGroup_name = "$sp_siteCollection_name Members"
$ViewersGroup = "Viewers"

# ---- CREATE SITE COLLECTION BLOCK ----

#if Managed path is Explicit than use this Create Site Collection, first check:
$sp_used_mangedPaths = Get-SPManagedPath -WebApplication $sp_webapp_name_URL
		foreach ($path in $sp_used_mangedPaths)
		{
			if (($path.Name -eq $sp_siteCollectionManagedPath) -and ($path.Type -eq 'ExplicitInclusion'))
			{
			$managedPath_Explicit = 'Y'
			}
			else
			{
			$managedPath_Explicit = 'N'
			}
		}

if ($managedPath_Explicit -eq 'Y'){
	$sp_siteCollection_Explicit = $sp_webapp_name_URL + '/' + $sp_siteCollectionManagedPath
	New-SPSite -Url $sp_siteCollection_Explicit –ContentDatabase $sp_contentdatabase_name  –Description $sp_siteCollection_description  -Template $sp_siteCollection_template_code -OwnerAlias $sp_website_primaryLogin_name –OwnerEmail $sp_website_primaryEmail_name -SecondaryOwnerAlias $sp_website_secondaryLogin_name -SecondaryEmail $sp_website_secondaryEmail_name
	$sp_createdSiteCollection_URL = $sp_siteCollection_Explicit
	}
	else
	{
	#Create Site Collection:
	New-SPSite -Url $sp_siteCollection_URL –ContentDatabase $sp_contentdatabase_name -Name $sp_siteCollection_name –Description $sp_siteCollection_description  -Template $sp_siteCollection_template_code -OwnerAlias $sp_website_primaryLogin_name –OwnerEmail $sp_website_primaryEmail_name -SecondaryOwnerAlias $sp_website_secondaryLogin_name -SecondaryEmail $sp_website_secondaryEmail_name
	$sp_createdSiteCollection_URL = $sp_webapp_name + $sp_siteCollection_name
	}
$web = Get-SPWeb $sp_createdSiteCollection_URL
##Associate deafault groups and administrators to Site Collection:
$web.CreateDefaultAssociatedGroups($sp_website_primaryLogin_name,$sp_website_secondaryLogin_name,"")
$sp_SiteCollection_PrimaryAdmin = Get-SPUser $sp_website_primaryLogin_name -Web $sp_createdSiteCollection_URL
$sp_SiteCollection_PrimaryAdmin.Name = $sp_website_primaryDisplay_name
$sp_SiteCollection_PrimaryAdmin.Update()
$sp_SiteCollection_SecondaryAdmin = Get-SPUser $sp_website_secondaryLogin_name -Web $sp_createdSiteCollection_URL
$sp_SiteCollection_SecondaryAdmin.Name = $sp_website_secondaryDisplay_name
$sp_SiteCollection_SecondaryAdmin.Update()
# Finish by disposing of the SPWeb object
$web.Dispose()

Read-Host -Prompt 'Finished....?:'

Suggestions and Ideas for this Script are welcome!

2 thoughts on “Creating SharePoint 2010 Site Collections with a Powershell Script

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>