Grant permission on Document library using CSV through powershell

JamaliSara 80 Reputation points
2023-06-16T00:36:38.2066667+00:00

Is there any script to grant permission on Document library using CSV

I search every but not found any script regarding this

I tried the following script but it only work for list as well as need to write list name one by one on script So it was not usefull for me

Config Variables
$SiteURL = "https://domain.sharepoint.com/sites/Amy12345/"
$ListName ="library 1"

$filePath = "C:\Users\spadmin\Desktop\pnp.csv" 

$csv = Import-CSV $filePath
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive  
 
#Break Permission Inheritance of the List
Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments
 
#Grant permission on Library to User
ForEach($Row in $csv) {
         Set-PnPListPermission -Identity $ListName -AddRole $Row.permission -User $Row.user
}
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-06-16T01:42:49.7566667+00:00

    Is this what you're trying to do? You can read the list names from a file instead of using an array if you don't want to make changes to the script when the list changes.

    #Config Variables
    $SiteURL = "https://domain.sharepoint.com/sites/Amy12345/"
    $ListNames ="library 1","library 2","library 3"
    
    $filePath = "C:\Users\spadmin\Desktop\pnp.csv" 
    
    $csv = Import-CSV $filePath
     
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive  
    
    ForEach ($ListName in $ListNames){
        #Break Permission Inheritance of the List
        Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments
     
        #Grant permission on Library to User
        ForEach($Row in $csv) {
            Set-PnPListPermission -Identity $ListName -AddRole $Row.permission -User $Row.user
        }
    }
    

  2. Rich Matheisen 47,901 Reputation points
    2023-06-16T15:08:00.9333333+00:00

    You didn't mention that the library name was in the CSV. If you give an incomplete description of the problem and the data you'll get incomplete answers.

    There is a way to do the Set-PnPList only once for each library by using another loop, but I don't know if that's necessary.

    #Config Variables
    $SiteURL = "https://domain.sharepoint.com/sites/Amy12345/"
    $filePath = "C:\Users\spadmin\Desktop\pnp.csv" 
    
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive  
    
    #Grant permission on Library to User
    Import-CSV $filePath {
        #Break Permission Inheritance of the List
        Set-PnPList -Identity $_.Library -BreakRoleInheritance -CopyRoleAssignments
        Set-PnPListPermission -Identity $_.Library -AddRole $_.permission -User $_.user
    }
    
    0 comments No comments

Your answer

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