SharePoint Online: Grant Folder Permissions from a CSV File using PowerShell

Requirement: Set permissions to Multiple Folders from a CSV File in SharePoint Online.

PowerShell to Set Folder Permissions in Bulk from a CSV File in SharePoint Online

You can Set folder permissions in SharePoint Online document library using the web browser interface or PowerShell. In this article, let’s see how to use PowerShell to set permissions on multiple folders in SharePoint Online from a CSV file. This can be helpful if you need to give specific users access to a set of folders quickly. This guide will show how to set folder permissions in SharePoint Online from a CSV file using PowerShell.

We have a list of folders in SharePoint Online libraries and wanted to grant permissions on specific folders with specific users. Here is my CSV File:

PowerShell to grant permission to folders in SharePoint Online from csv File

Here is the PowerShell script to set folder permissions from a CSV:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$CSVFile = "C:\Temp\FolderPermissions.csv"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the CSV file
    $CSVData = Import-CSV $CSVFile
 
    #Read CSV file and grant folder permissions
    ForEach($Row in $CSVData)
    {
        Try {
            #Get the Folder
            $Folder = Get-PnPFolder -Url $Row.FolderServerRelativeURL -Includes ListItemAllFields.ParentList -ErrorAction Stop
            $List =  $Folder.ListItemAllFields.ParentList
            #Get Users
            $Users =  $Row.Users -split ";"
            ForEach($User in $Users)
            {
                #Grant Permission to the Folder
                Set-PnPFolderPermission -List $List -Identity $Folder.ServerRelativeUrl -User $User.Trim() -AddRole $Row.Permission -ErrorAction Stop
                Write-host -f Green "Ensured Permissions on Folder '$($Row.FolderServerRelativeURL)' to '$($User.Trim())'"
            }
        }
        Catch {
            Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
        }
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Please note, this script appends permission to the existing permissions on the folders. If you want to clear all existing permissions and grant only the given permissions from the CSV, use the “-ClearExisting” switch to the Set-PnPFolderPermission cmdlet.

In case, You want to grant permission to SharePoint groups instead of a user account, use the following:

Set-PnPFolderPermission -List $ListName -identity $SubFolder.ServerRelativeURL -AddRole "Edit" -Group "Marketing Members"

Conclusion

In conclusion, granting folder permissions in SharePoint Online can be time-consuming and repetitive if done manually. However, by using a CSV file and PowerShell, you can grant folder permissions to multiple users in bulk and automate the process to grant folder permissions to multiple users in just a few minutes. With the script provided in this article, you can automate the process, save time and resources, and ensure that the permissions are applied consistently and accurately.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

11 thoughts on “SharePoint Online: Grant Folder Permissions from a CSV File using PowerShell

  • I keep getting an error file not found. Hoping I’m doing something simple that’s wrong.

    $SiteURL = “https://company.sharepoint.com/sites/Teamtesi”

    sites/Teamtesi/Shared%Documents/pathtesting/Fund_property/PropertyName/Acquisition/Closing%Documents
    sites/Teamtesi/Shared Documents/pathtesting/Fund_property/PropertyName/Acquisition/Legal

    Reply
    • It is the same as my issue the first time running.
      After changing *.csv as below, it works successfully.

      FolderServerRelativeURL
      /sites/site_test/Shared Documents/Temp_folder

      Reply
  • How to use this with AAD groups instead of Sharepoint groups ?

    Reply
    • For user ID, You can use the AAD Group ID something like: $AdGroupID = “c:0t.c|tenant|798cb3d4-7ca8-4567-adb5-916bc496d7cd”

      Reply
  • Hello Salaudeen Rajack.
    First of all, thanks a million for your contribution.

    I need to add permissions to diferent sites, I need to change somethink, becouse, there it`s works to add permission to folders into a site.-

    Regards.

    Reply
  • Hi, Is there a way to exclude “Send an email Invite” for while adding the users/groups permissions? Thanks

    Reply
    • The Set-PnPFolderPermission cmdlet doesn’t send any Email notifications by default!

      Reply
  • Hi again – scratch that. My subsite had spaces and were replaced by SPO by %20. As soon as I removed it it worked. Thanks again!

    Reply
  • Hi – does this work when targeting subsites? I have no issues when I am testing it on site level. On subsites I receive “Error: file not found”.

    Thank you

    Reply
  • this will be very useful, thanks. If I want to disable inheritance of permissions on a folder in the CSV, and then set the permissions, how could we tweak the script to enable that per row?

    Reply
    • The Set-PnPFolderPermission cmdlet automatically breaks the folder’s permission inheritance – if it’s not using unique permissions already!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *