About sharepoint permission level

JamaliSara 80 Reputation points
2023-06-14T08:43:39.93+00:00

I want to ask if there is any way to grant Permission to the user using CSV

through Powershell.

I run some script it will grant permission to user bt it took time to write user name so is there any script to grant permmison to user through script

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. Emily Du-MSFT 51,951 Reputation points Microsoft External Staff
    2023-06-15T02:39:20.83+00:00

    1.Create a csv file as following picture show.

    Screenshot 2023-06-15 103643

    2.Please run below PNP PowerShell.

    
    $SiteURL = "https://tenant.sharepoint.com/sites/emilytest"
    
    $filePath = "C:\pnp.csv" 
    
    $csv = Import-CSV $filePath
    
    Connect-PnPOnline -Url $SiteURL -Interactive   
     
    ForEach($Row in $csv) {
             Set-PnPWebPermission -User $Row.user -AddRole $Row.permission
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Shabana Sohail 326 Reputation points
    2023-06-14T17:08:08.6633333+00:00

    Yes, you can grant permission to users in SharePoint using a CSV file through PowerShell. You can use the Import-CSV cmdlet to import the data from the CSV file and then use a ForEach loop to iterate through each row of the CSV file and grant permission to the users using the Set-PnPFolderPermission cmdlet. Here is an example script that sets folder permissions from a CSV file in SharePoint Online:

    #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
    }
    
    

    This script reads data from a CSV file and grants permission to users on specific folders with specific permissions. You can modify this script according to your needs. Please note that 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 with the SetPnPFolderPermission cmdlet

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.