Create Many Site Collections via PowerShell


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:


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.

# Concept borrowed from Gary Lapointe:
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:
		$MembersGroup = "$_.Name Members"
		$ViewersGroup = "Viewers"
		$web = Get-SPWeb $_.url
		$PrimaryAdmin = Get-SPUser $_.OwnerLogin -Web $_.url
		$PrimaryAdmin.Name = $_.OwnerDisplay
		$SecondaryAdmin = Get-SPUser $_.SecondaryLogin -Web $_.url
		$SecondaryAdmin.Name = $_.SecondaryDisplay

		# Finish by disposing of the SPWeb object to be a good PowerShell citizen

# Execute the script			

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:


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:

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 `
            Stop-SPAssignment -SemiGlobal $gc

# Execute the script			

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.

0 comments… add one

Leave a Reply