Set Unique Permissions in SharePoint Libraries with PowerShell

Managing permissions in SharePoint can be tricky, especially if you have many libraries and lists. Automating this process can save you a lot of time and effort. In this post, we’ll walk through a PowerShell script that helps you set permissions for libraries with unique permissions, filtered by their titles.

The Script

Here’s a simple script that defines a function called TRPD-SetPermissions. This function takes four inputs: the site URL, user login, user role, and a part of the library title you want to filter by. It searches for libraries with unique permissions and sets the specified permissions for the user.

Function TRPD-SetPermissions($SiteURL, $userLogin, $userRole, $libraryTitle = $null)
{
    # Function to Get Lists with Unique Permissions from the web
    Function Get-PnPUniquePermissionLists([Microsoft.SharePoint.Client.Web]$Web)
    {
        Write-host "Searching Lists and Libraries with Unique Permissions at:" $Web.Url -f Yellow
        # Connect to the SharePoint site
        # Connect-PnPOnline -Url $Web.URL -Interactive
        # Get all lists from the site
        $Lists = Get-PnPList -Includes HasUniqueRoleAssignments
     
        # Exclude system lists
        $ExcludedLists = @("Content and Structure Reports","Form Templates","Images","Pages","Preservation Hold Library", "Site Pages", "Site Assets",
                             "Site Collection Documents", "Site Collection Images","Style Library","Reusable Content","Workflow History","Workflow Tasks")
               
        # Iterate through lists
        ForEach($List in $Lists)
        {
            # Filter lists - Exclude system lists, hidden lists, and get only lists with unique permissions
            If($List.Hidden -eq $False -and $ExcludedLists -notcontains $List.Title -and $List.HasUniqueRoleAssignments -and ($libraryTitle -eq $null -or $List.Title -like "*$libraryTitle*"))
            {
                Set-PnPListPermission -Identity $List.Title -User $userLogin -AddRole $userRole

                Write-host "`tFound a List '$($List.Title)' with Unique Permission at '$($List.RootFolder.ServerRelativeUrl)'" -f Green
                Write-Output "Granted $userRole access to $userLogin for library $($List.Title)"
            }
        }
    }
    # Call the function for each web
    Get-PnPWeb | ForEach-Object { Get-PnPUniquePermissionLists($_)}    
}

How It Works

Parameters: The function accepts four inputs:

$SiteURL: The URL of your SharePoint site.

$userLogin: The login name of the user you want to set permissions for.

$userRole: The role you want to assign to the user (e.g., Read, Contribute).

$libraryTitle: (Optional) A part of the library title to filter by. If not provided, the function sets permissions for all libraries with unique permissions.

Get Lists: The script retrieves all lists from the specified site.

Filter Lists: It excludes system lists and hidden lists, and filters by unique permissions and the specified title.

Set Permissions: For each matching list, it sets the specified permissions for the user.

Benefits

Efficiency: Automates the process of setting permissions, saving you time.

Accuracy: Reduces the risk of errors by automating repetitive tasks.

Flexibility: Allows filtering by library title, making it adaptable to different scenarios.

Examples

  1. Set Read Access for All Libraries with Unique Permissions:
    • TRPD-SetPermissions -SiteUrl “https://yoursharepointsite” -userLogin “user@example.com” -userRole “Read”
  2. Set Contribute Access for Libraries Containing “Projects” in their Title:
    • TRPD-SetPermissions -SiteUrl “https://yoursharepointsite” -userLogin “user@example.com” -userRole “Contribute” -libraryTitle “Projects”

Automating SharePoint permissions with PowerShell can greatly improve efficiency and accuracy. By using the script above, you can easily manage permissions for libraries with unique permissions, filtered by title. Avoiding common mistakes will help ensure the script runs smoothly.

Feel free to customize the script to suit your needs and let us know how it works for you!

Leave a comment