Hi @Ganesh ,
The following script will use PSExcel to read excel data like the pic
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.