PowerShell script to create sub-sites with unique permission from the SharePoint List

Today, I came across one interesting question in the MSDN SharePoint forums on creating sub-sites from the list.

Scenario

User has list with four columns SiteName, Email, ManagerName, ManagerEmail as shown in the below screenshot.

SiteColumns

 

From the above List, user want to create the sub-sites with unique permissions, added Manager as user with Contribute permission.

Solution

PowerShell Script to access list and create sub-sites with unique permission.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$sourceWebURL = "http://sp2013" #Replace with your site collection
$sourceListName = "MySites" #Replace with your List
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$spSourceItems = $spSourceList.GetItems()
foreach($item in $spSourceItems)
{
$siteTitle = $item["Title"]
$siteName = $siteTitle -replace " ", ""
$NewSiteUrl = $sourceWebURL+"/"+$siteName
write-host "Title:"$siteTitle
write-host "Url:"$NewSiteUrl

#Create User Object from the List Item
$userWithContribute = New-Object Microsoft.SharePoint.SPFieldUserValue($spSourceWeb,$item["ManagerName"].ToString());

#Create Sub-site with Unique Permissions
$newWeb = New-SPWeb -Url $NewSiteUrl -Name $siteTitle -Template "STS#0" -UniquePermissions

#Assign Manager with Contribute permission
Set-SPUser -Identity $itemUser.User -Web $newWeb -AddPermissionLevel "Contribute"

}

Same can be downloaded from the Technet Gallery

This entry was posted in SharePoint 2013. Bookmark the permalink.

Leave a Reply