Streamline File and Folder Permissions Management in SharePoint with PowerShell

In this post, we will provide a PowerShell script to help manage file and folder permissions in SharePoint. Proper management of permissions is essential for ensuring that users have the appropriate access while maintaining security. This script allows administrators to quickly grant or revoke permissions for specific users on SharePoint document libraries, making it easier to maintain control over sensitive information.
Here is the PowerShell script for managing SharePoint permissions:

# Connect to SharePoint Online
$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$credential = Get-Credential
Connect-PnPOnline -Url $siteUrl -Credentials $credential
# Define the file or folder URL and the user to manage permissions
$itemUrl = "/sites/YourSite/Shared Documents/YourFile.txt"
$userEmail = "[email protected]"
$role = "Edit"  # Options: "View", "Edit"
# Grant permissions to the user
Set-PnPListItemPermission -List "Shared Documents" -Identity $itemUrl -User $userEmail -AddRole $role
Write-Host "Permissions have been granted to $userEmail for the file at $itemUrl with the role $role."