kwizcom banner advertisement

SharePoint Online: Set Folder Permissions using PowerShell CSOM

Requirement: Change folder permissions in SharePoint Online using PowerShell.

PowerShell to change folder level permissions sharepoint online:
This PowerShell script breaks permissions of a folder and grants permissions using client side object model (CSOM).
#Load SharePoint CSOM Assemblies
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"
$FolderURL="/Project Documents/Active"
$GroupName="Team Site Members"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.web

    #Get the Folder
    $Folder = $Web.GetFolderByServerRelativeUrl($FolderURL)
    #Break Permission inheritence - Remove all existing list permissions & keep Item level permissions
    Write-host -f Yellow "Folder's Permission inheritance broken..."
    #Get the SharePoint Group & User
    $Group =$Web.SiteGroups.GetByName($GroupName)
    $User = $Web.EnsureUser($UserAccount)

    #Grant permission
    #Get the role required
    $Role = $web.RoleDefinitions.GetByName($PermissionLevel)
    $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
    #Assign permissions
    $GroupPermissions = $Folder.ListItemAllFields.RoleAssignments.Add($Group,$RoleDB)
    $UserPermissions = $Folder.ListItemAllFields.RoleAssignments.Add($User,$RoleDB)
    Write-host "Permission Granted Successfully!" -ForegroundColor Green  
Catch {
    write-host -f Red "Error Granting permission to  Folder!" $_.Exception.Message
and the result:
sharepoint online set folder permissions
Tags: folder level permissions in sharepoint online, sharepoint online change folder permissions, sharepoint online document library folder permissions, sharepoint online manage folder permissions, sharepoint online set permissions on folder, sharepoint online restrict access to folder
SharePoint Online: Set Folder Permissions using PowerShell CSOM SharePoint Online: Set Folder Permissions using PowerShell CSOM Reviewed by Salaudeen Rajack on September 19, 2016 Rating: 5


  1. Merci beaucoup, damn useful, using it for O365

  2. Hi there!

    I'm facing below error,
    Error Granting permission to Folder! Exception calling "ExecuteQuery" with "0" argument(s): "Server relative urls must start with SPWeb.ServerRelativeUrl"

    Any idea why?

    1. Most likely you are trying to break the inheritance for a site different than the root site collection.
      try to use the following format for the FolderURL:
      $FolderURL = "/sites///"
      $FolderURL = "/sites/MyTestSite/MyTestLibrary/MyTestFolder"


Please Login and comment to get your questions answered!

Powered by Blogger.