I have a document library with a large amount of directories and thus permissions which I would like to break inheritance of and have come across the SharePointDiary script which works perfectly apart from the SharePoint implemented list threshold limit.

To work around this I have been moving files/directories, running said script in short batches and then moving data back. However, that being said, sometimes my operation will time out. Could someone walk me through altering the script below to either run in batches to circumvent the list threshold limit or stop the operation from timing out?


Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for Processing
$SiteURL = "https://contoso.sharepoint.com/site/"
$ListName = "DOCUMENTS"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
   #Setup the context
   $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
   $Ctx.Credentials = $Cred
   #Get the List
   #Get All List Items
   $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
   $Query.ViewXml ="<View Scope='RecursiveAll' />"
   $ListItems = $List.GetItems($Query)

   #Loop through each list item
   For($i=0;$i -lt $ListItems.Count;$i++)
       #Break Inheritance copying permissions from parent
       $ListItems[$i].BreakRoleInheritance($True, $False)
   write-host  -f Green "Permission Inheritance Broken for All Items in the List '$ListName'"
Catch {
   write-host -f Red "Error:" $_.Exception.Message```
  • How many directories do you have? I'm not sure breaking inheritance on that many folder level items is wise. May 24, 2022 at 21:11
  • It's a sizable file share that that is being migrated to the cloud. The group permissions are synchronized with AD. May 24, 2022 at 21:34
  • I would recommend more analysis and splitting the folders/files in to many SharePoint sites/libraries according to their purpose in the organisation. Unique permissions on thousands of folders sounds like it will cause a lot of management issues down the line. May 24, 2022 at 23:01
  • Correct - there are approximately 85 top level directories that have been broken down into separate libraries. Due to granularized on site permissions and the fact that fixing the permissions layout is out of my hands I have to work with what I was dealt and insure that there is no likelihood someone may inherit access to inappropriate directories. May 25, 2022 at 11:42
  • That's a shame, seems like this definitely requires a rethink of the permissions here. May 25, 2022 at 12:11


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.