Create Many Site Collections via PowerShell

SharePoint

I had a problem this week where I needed to create many (like 30 or so) site collections in SharePoint 2010. These sites all have similar properties but different names and URLs of course. I also needed a way to remove all the sites I had just created easily.

New-SPSite and Remove-SPSite do the job, but when you have lots of site collections, you can’t be repetitive. Luckily for me, similar scripts have been written to handle my requirements so I just needed to hack them together. I used an excellent post by Gary Lapointe (@glapointe): Creating A SharePoint 2010 Site Structure Using PowerShell. I also needed to make some minor tweaks because results using PowerShell differs slightly from when you use Central Administration. Dan Holme (@danholme) provides those tweaks here: Create A SharePoint Site Collection With Windows PowerShell, UI Style.

Let’s get started!

First, we’ll create an XML file like the one Gary Lapointe shows. Since I only need to create Site Collections, I’ve removed everything else. Here’s how my XML file looks, named sites.xml:

?Download sites.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<SiteCollections>                    
	<SiteCollection Name="ABC Test"
					Description=""
					Url="http://corp.contoso.com/sites/abctest"
					LCID="1033"
					Template="STS#1"
					OwnerLogin="CORP\wsaleemi"
					OwnerEmail="wsaleemi@contoso.com"
					OwnerDisplay="Wahid Saleemi"
					SecondaryLogin="CORP\spfarm"
					SecondaryEmail="spfarm@contoso.com"
					SecondaryDisplay="SharePoint Farm">
	</SiteCollection>
	<SiteCollection Name="ABC Test 2"
					Description=""
					Url="http://corp.contoso.com/sites/abctest2"
					LCID="1033"
					Template="STS#1"
					OwnerLogin="CORP\wsaleemi"
					OwnerEmail="wsaleemi@contoso.com"
					OwnerDisplay="Wahid Saleemi"
					SecondaryLogin="CORP\spfarm"
					SecondaryEmail="spfarm@contoso.com"
					SecondaryDisplay="SharePoint Farm">
	</SiteCollection>
</SiteCollections>

As you can see, we just copy all the lines between <SiteCollection> and </SiteCollection> for each new Site Collection we need. Next, specify the values you need for Name, Description, URL, etc.

Next, we need a script to create these sites. The script will loop through each <SiteCollection> block and create the site based on the values we’ve specified here. Nothing in the script needs to be changed because all our values are derived from the XML file above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Concept borrowed from Gary Lapointe: http://blog.falchionconsulting.com/index.php/2009/12/creating-a-sharepoint-2010-site-structure-using-powershell/
function Start-SiteCollectionCreate(
    [string]$settingsFile = "sites.xml") {
    [xml]$config = Get-Content $settingsFile
 
    $config.SiteCollections.SiteCollection | ForEach-Object {
        #Creating site collection
        Write-Host "Creating site collection $($_.Url)..."
        $gc = Start-SPAssignment
        $site = $gc | New-SPSite `
            -Url $_.Url `
            -Description $_.Description `
            -Language $_.LCID `
            -Name $_.Name `
            -Template $_.Template `
            -OwnerAlias $_.OwnerLogin `
            -OwnerEmail $_.OwnerEmail `
            -SecondaryOwnerAlias $_.SecondaryLogin `
            -SecondaryEmail $_.SecondaryEmail
            Stop-SPAssignment -SemiGlobal $gc
		# Associate Default Groups (Dan Holme: http://www.sharepointpromag.com/article/sharepoint/Create-a-SharePoint-Site-Collection-with-Windows-PowerShell-UI-Style)
		$MembersGroup = "$_.Name Members"
		$ViewersGroup = "Viewers"
		$web = Get-SPWeb $_.url
		$web.CreateDefaultAssociatedGroups($_.OwnerLogin,$_.SecondaryLogin,"")
		$PrimaryAdmin = Get-SPUser $_.OwnerLogin -Web $_.url
		$PrimaryAdmin.Name = $_.OwnerDisplay
		$PrimaryAdmin.Update()
		$SecondaryAdmin = Get-SPUser $_.SecondaryLogin -Web $_.url
		$SecondaryAdmin.Name = $_.SecondaryDisplay
		$SecondaryAdmin.Update()
 
		# Finish by disposing of the SPWeb object to be a good PowerShell citizen
		$web.Dispose()
        }
	}
 
# Execute the script			
Start-SiteCollectionCreate

Note near the top of the file we have $settingsFile set to “sites.xml.” Save this file as something like “CreateSitesXML.ps1” and then execute it in the SharePoint 2010 Management Shell:

?View Code POWERSHELL
1
.\CreateSitesXML.ps1

Finally, I wanted to remove all those sites that I just created. This is really cumbersome in Central Administration. We can use Remove-SPSite in PowerShell, but since I already have a list of sites in my XML file, lets use that instead. So here’s a script to remove those same sites:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Start-SiteCollectionRemove(
    [string]$settingsFile = "sites.xml") {
    [xml]$config = Get-Content $settingsFile
 
    $config.SiteCollections.SiteCollection | ForEach-Object {
        #Removing site collection
        Write-Host "Removing site collection $($_.Url)..."
        $gc = Start-SPAssignment
        $site = $gc | Remove-SPSite `
            -Identity $_.Url `
            -Confirm:$false
            Stop-SPAssignment -SemiGlobal $gc
        }
	}
 
# Execute the script			
Start-SiteCollectionRemove

Name this script something like “RemoveSitesXML.ps1” and run it the same way you did to create sites.

Another note, if you look at the blog posts I referenced, you’ll see a value for “Content Database” in those versions. Add it back in if you need but for me, it was more of a bother because I didn’t have any requirements to use different databases.

email
0 comments… add one

Leave a Comment

%d bloggers like this: