SharePoint Online: Set Folder Permissions using PowerShell

Requirement: Change folder permissions in SharePoint Online using PowerShell.

How to Set folder level permissions in SharePoint Online?
How to give unique permission to a folder in SharePoint Online? To manage folder permissions such as Add or Restrict in SharePoint Online, Follow these steps:
  • Navigate to your SharePoint Online document library where the target folder is located. 
  • Click on "Details" from the specific folder's context menu >> In the Details pane, Click on "Manage Access" and then "Advanced" links. This takes you to the "Advanced Permissions" page
  • From the ribbon, Click on "Stop Inhering Permissions" button and confirm the prompt.
  • Now, You'll get the list of users and groups who already have permissions on the folder. When you break the permission, SharePoint copies permissions from its parent (List/library in our case!). Click on "Grant Permission" button from the ribbon. 
    powershell sharepoint online add permission to folder
  • Enter the names of the users and groups you want to add permission to the folder, Select the appropriate permission level by clicking on "Show Options" link in the share page. Click on "Share" button to add permission to folder.
    sharepoint online set permissions on folder
Alright! Let's use PowerShell to add user to SharePoint Online folder.

PowerShell to change folder level permissions SharePoint online:
Lets add permission to SharePoint Online folder using PowerShell. 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" #Relative URL of the Folder!
$GroupName="Team Site Members"
$UserAccount="[email protected]"

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 of the folder - Keep all existing folder 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)

    #sharepoint online powershell set permissions on folder
    #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
This PowerShell breaks permission inheritance of the folder and grants access to a user and group. Here is the result of change folder permissions:
sharepoint online set folder permissions
To Remove User or Group from Folder in SharePoint Online, Use this PowerShell script: How to Remove a User or Group from Folder Permissions in SharePoint Online?

If you want to set permissions on all folders in a SharePoint Online document library, refer: SharePoint Online: Grant Permission to Each Folder in a Document Library using PowerShell

PnP PowerShell to Change Folder Permissions in SharePoint Online
To grant permission to folder in SharePoint Online, use this PnP PowerShell:
#Config Variables
$SiteURL = ""
$FolderServerRelativeURL = "/sites/marketing/Shared Documents/2019"
$UserAccount = "[email protected]"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Folder from URL
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL
#Set Permission to Folder
Set-PnPListItemPermission -List $ListName -Identity $Folder.ListItemAllFields -User $UserAccount -AddRole 'Contribute'

How to restrict access to a folder in SharePoint Online?
You can also restrict access to a SharePoint Online folder using Set-PnPfolderPermission cmdlet. Here is an example:
#Config Variables
$SiteURL = ""
$ListName ="Branding"
$FolderServerRelativeURL = "/Sites/Marketing/Branding/2020"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Set folder permissions - Add User
Set-PnPfolderPermission -List $ListName -identity $FolderServerRelativeURL -User "[email protected]" -AddRole "Edit"

#To remove user, use: Set-PnPfolderPermission -List $ListName -identity $FolderSiteRelativePath -User "[email protected]" -RemoveRole "Edit"

How to change folder permissions in SharePoint Online with PnP PowerShell?
Similarly, to grant permission to a SharePoint Online group, use:
#Config Variables
$SiteURL = ""
$ListName ="Branding"
$FolderServerRelativeURL = "/Sites/Marketing/Branding/Active"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Grant folder permissions to SharePoint Group
Set-PnPfolderPermission -List $ListName -identity $FolderServerRelativeURL -AddRole "Edit" -Group "Marketing Members"
This script breaks the permission inheritance of the given folder and add or remove permission to the folder to given user or group. Similarly, you can set permissions to AD Groups using its name as parameter value to "user". Here is my another post on SharePoint Online: PowerShell to Get Folder Permissions


  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"

    2. Thanks for the suggestion, my scenario is actually a folder inside a sub-site (not the root site), I tried the '/sites///' format without luck.
      Exception calling "ExecuteQuery" with "0" argument(s): "Server relative urls must start with SPWeb.ServerRelativeUrl"
      It is preventing me from proceeding. Any other ideas?

    3. Found the fix after two months, removed the '/' from the beginning of the folder name. It became something like this:

    4. The $SiteURL parameter and Folder's ServerRelativeURL must be the same web Context!

  3. Is there a way to use this code to remove the permission if my folder already has the permission?

  4. I have given Full Control to the spesific user but I can not see any changes even after logging from that spesific user to access the folder. The user can not change, delete or edit the folder but strangely it shows Full Control in the folder permission list.

  5. I'm receiving the following:
    Error Granting permission to Folder! Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found."

    Any help, much appreciated.

    1. Here the Folder URL should be relative URL. Say: Your Site collection is at:" and your folder is at: /documents/active", then the relative URL should be: "/sites/sales/documents/active"

    2. Same problem for me - trying to loop through a number of "subsites" and setting the permission on a folder in their default documents library.

      When using this advise: "Here the Folder URL should be relative URL. Say: Your Site collection is at:" and your folder is at: /documents/active", then the relative URL should be: "/sites/sales/documents/active"

      I get error: Error Granting permission to Folder! Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found." when using

    3. Please note, your document library's name could be "Documents" while the actual URL is: /shared documents/, So check if the given relative URL is valid in $FolderURL parameter!

    4. Hi again. It seem that the command
      $Group =$Web.SiteGroups.GetByName($GroupName)
      gets the SharePoint Groups only. What can we do if we want to get and Active Directory Synched group?

    5. Use: $ADGroup = $Web.EnsureUser("Domain\ADGroup")

    6. Trying to do the same but I'm not using AD Connect. I've created groups in the admin portal and they are referred to as a domain group when assigned just through the Web gui. Is my Domain just my tennant that is created? Sorry if this is a silly question. And thank you for this tutorial.

    7. If you have custom domain and mapped UPN to that domain, then Its [email protected], Otherwise, [email protected]

  6. Hello.
    I am getting the error: Exception calling "ExecuteQuery" with "0" argument(s): "Can not find the principal with id: 259."
    for line 48 when $Ctx.ExecuteQuery() is being executed to add the permisison to the folder.

    How can this be resolved?

    Thanks in advance

    1. Chances are: The given User ID is orphaned or External User ID!

  7. It doesnt seem to work with external users...

  8. Thank you! Great article
    Any idea how to give permissions in a folder to a sharepoint group? I tried -group parameter but It gives an error like: Set-PnPFolderPermissions: It can't find this permission level (my translation from spanish)

    1. Group parameter is meant for "SharePoint Groups" (Not AD Group!), If you want to grant permission to AD group (Security/Office 365 groups), use -User parameter with the group name.

  9. Is there a way to grant access to all SharePoint online sites for an account using Power shell

  10. Thank your for your scripts, Salaudeen you are so great doing this.
    I was having issues, but using the -Includes ListItemAllFields resolved it:
    $Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields

    best regards!

    1. Hi.
      I used your additional update although it still shows Get-PnPFolder: File Not Found.
      In the -SetPnPItemListPermission with the -Identity attribute, is there something to update or not after this.

  11. Salaudeen, your posts are well done, thank you!

    I'm making a script (1800+ lines) that assigns unique permissions to folders based on security groups in O365. Recently in my testing I'm getting errors when I assign "Restricted View" to some folders around the end of the script. If I assign by hand sometimes it works fine other times it doesn't. It use to work fine as all of my previous scripts have the appropriate right set. The command I'm using is:

    Set-PnPFolderPermission -List "Shared Documents" -Identity "Shared Documents/General/Private Stuff" -User Proj-Special-Access -AddRole "Restricted View"

    Any help would be super appreciated.

    1. Some additional information: After I've run the script and my login has expired I go back to my script and do the following (quotes indicate exact commands):

      Execute initial portion that sets variables
      Get team name and groupID variables
      "Connect-PnPOnline -ClearTokenCache -SPOManagementShell $SharePointSiteName #This logs in with MFA
      Execute permission command in original post

      This executes properly.


Please Login and comment to get your questions answered!

Powered by Blogger.