Need to Remove multiple users from multiple Document librarys using CSOM/ Powershell script

Ganesh 46 Reputation points
2022-02-14T07:11:23.257+00:00

Hi Team,

We have multiple Document libraries having unique permissions each in SPO.

I need to remove users from those Libraries. Doing manually by going to each site one by one taking lot of time

Can some assist using CSOM or PowerShell to remove users for each respective Library by passing excel Input with user ID and SP Doc would be helpful.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,987 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 35,631 Reputation points Microsoft Vendor
    2022-02-15T09:35:03.207+00:00

    Hi @Ganesh
    The following script will use PSExcel to read excel data like the pic
    174464-image.png

    Install-Module PSExcel  
      
    $path = "c:\document\SPuser.xlsx"  
      
    import-module psexcel #it wasn't auto loading on my machine  
      
    #Connect PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
      
    $file=Import-XLSX -Path $path -RowStart 1  
    foreach ($user in $file){  
    #Config Variables  
    $SiteURL = "https://xxx.sharepoint.com/sites/test"  
    $ListName = $user.Library  
    $UserID= $user.UserID  
       
    #Get the Context  
    $Context = Get-PnPContext  
        
    #Get the list & User objects  
    $List = Get-PnPList -Identity $ListName  
    $User = Get-PnPUser -Identity $UserID  
       
    #Break Permission Inheritance  
    Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments  
       
    #Remove User from List Permissions  
    $List.RoleAssignments.GetByPrincipal($User).DeleteObject()  
    $Context.ExecuteQuery()  
      
    }  
    

    ---------------------------------------------------
    Updated---------------------------------------------------
    You can use csom to access sharepoint by following script. The UserID in excel should change to logon name like xxx@abc.com

    $path = "c:\document\SPuser.xlsx"  
      
    import-module psexcel #it wasn't auto loading on my machine  
      
    $SiteURL = "https://xxx.sharepoint.com/sites/test"  
    $Cred = Get-Credential  
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)  
        
    $file=Import-XLSX -Path $path -RowStart 1  
    foreach ($user in $file){  
    #Config Variables  
      
    $ListName = $user.Library  
    $UserAccount= $user.UserID  
       
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
    $Ctx.Credentials = $Cred  
            
    #Get the List  
    $List=$Ctx.Web.Lists.GetByTitle($ListName)  
    $Ctx.Load($List)  
    $Ctx.ExecuteQuery()  
               
    #Get the User  
    $User = $ctx.Web.EnsureUser($UserAccount)  
    $Ctx.Load($User)  
    $Ctx.ExecuteQuery()  
        
    #Break Permission Inheritance  
    $List.BreakRoleInheritance($true, $false)  
    $Ctx.ExecuteQuery()  
       
    #Get List Permissions  
    $Ctx.Load($List.RoleAssignments)  
    $ctx.ExecuteQuery()  
               
    #Remove Group from List Permissions  
    $List.RoleAssignments.GetByPrincipal($User).DeleteObject()  
    $Ctx.ExecuteQuery()  
        
    write-host  -f Green "User '$UserAccount' has been Removed from List '$ListName'"  
    }  
    

    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.



0 additional answers

Sort by: Most helpful

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.