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 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. In case, 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:

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

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • 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