Create a Site Structure using PowerShell

Updated to reflect feedback from Wes MacDonald - https://social.technet.microsoft.com/profile/wes%20macdonald/

Another day, another excuse to write a PowerShell script! This time I was working with a customer who needed to create a Site Collection for their HR department with a large number of Sub-Sites (one for each team within the department), each Sub-Site required unique permissions. Rather than spending an hour doing this through the UI I put together a script to automate this.

Update the values highlighted and run, this will create a single Site Collection and a Sub-Site beneath for each of the department team names contained in text file referenced by the $Departments variable, it will remove any spaces from the names to ensure that the URL is valid and will use the original department name for the display name of the site. The parameters used to create the Site: New-SPSite and Sub-Site: New-SPWeb can be tweaked to match your particular requirements.

ASNP *SharePoint* -EA SilentlyContinue
$Owner = "CONTOSO\JohnDoe"
$WebAppURL = "https://intranet.contoso.com/"
$Departments= Get-Content "C:\Departments.txt"
$Site = New-SPSite ($WebAppURL + "Sites/" + "HR") -Template STS#0 -OwnerAlias $Owner
$Site.RootWeb.CreateDefaultAssociatedGroups($Owner,$null,$Site.Title)
Foreach ($Department in $Departments) {$Web = New-SPWeb -Url (($Site.URL) + "/" + $Department.replace(" ","")) -Name $Department -Template STS#0 -UniquePermissions;$Web.CreateDefaultAssociatedGroups($Owner,$null,$Web.Title)}

Example $Departments input file:

Brendan Griffin