Tuesday, April 19, 2011

Powershell Script to create subsites within a site collection

The powershell scriptlet below accepts Site Collection URL,  Site Collection Template, Site Collection Language and Sub Site Names as configurable parameters, it then goes through each subsite array and creates subsites for that particular Site Collection.

1. Copy and Paste the code below and save it as CreateSubSite.ps1, see highlighted yellow sections to change configurable values:

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL

#Creating Sub Sites in top site collection.
Write-Output " "
Write-Output "Creating Sub Sites"

$SiteCollectionURL = "http://localhost/sites/SPFix"

$SiteCollectionTemplate = "STS#0"

$SiteCollectionLanguage = 1033

$SubSites = @("Central Services", "Knowledge Base", "Service Center", "IT", "HR", "Finance")

for($i=0 ; $i -lt $SubSites.count ; $i++)
$SiteUrl = ""
$SiteUrl = $SiteCollectionURL + "/"
$SiteUrl = $SiteUrl += $SubSites[$i]
Write-Output " "
#Write-Output "Creating Site for " += $SubSites[$i]
Write-Output " "
New-SPWeb $SiteUrl -Template $SiteCollectionTemplate -Name $SubSites[$i]  -UseParentTopNav -Language $SiteCollectionLanguage
Write-Output " "
#Write-Output "Site Created for " += $SubSites[$i]
Write-Output " "

Remove-PsSnapin Microsoft.SharePoint.PowerShell

2. To automatically run the CreateSubSite.ps1 script as a batch utility, Copy and paste code below and save it with a .bat file extension

cd /d %~dp0
powershell -noexit -file ".\CreateSubSite.ps1" "%CD%"

Run the Script and Enjoy :)


  1. Thanks for the post. What else needs to be added to the script so that during the creation of each subsite a dedicated content database is also created for each of the subsites?

  2. You cannot have Content Databases per Sub-site but you can have it per site collection.

  3. Excellent post, one question how could I use this script and have it use unique permissions for each subsite as well.