SharePoint Online: Remove User or Group from List Permissions using PowerShell

Requirement: Remove user or group from list permissions in SharePoint Online using PowerShell
PowerShell to Remove user or group from list permissions in sharepoint online

PowerShell to Remove User from List Permissions in SharePoint Online
Here is the SharePoint Online PowerShell to remove permissions
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Remove User from List Permissions
Function Remove-SPOUserFromListPermission($SiteURL,$ListName,$UserAccount)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    Try {
        #Setup the context
        $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'"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$ListName = "Documents"
$UserAccount = "Salaudeen@TheCrescent.com"
 
#Call the function to Remove user from List Permissions
Remove-SPOUserFromListPermission -SiteURL $SiteURL -ListName $ListName -UserAccount $UserAccount

PnP PowerShell to Remove User from List Permissions:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$ListName ="Documents"
$UserID= "i:0#.f|membership|Salaudeen@TheCrescentTech.com"

#Connect PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#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()

SharePoint Online: Remove Group from List Permissions using PowerShell CSOM
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Remove Group from List Permissions
Function Remove-SPOGroupFromListPermission($SiteURL,$ListName,$GroupName)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    Try {
        #Setup the context
        $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 Group
        $Group = $Ctx.Web.SiteGroups.GetByName($GroupName) 
        $Ctx.Load($Group)
        $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($Group).DeleteObject()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "Group '$GroupName' has been Removed from List '$ListName'"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$ListName = "Documents"
$GroupName = "Sales Portal Members"
 
#Call the function to Remove group from SharePoint List permissions
Remove-SPOGroupFromListPermission -SiteURL $SiteURL -ListName $ListName -GroupName $GroupName

PnP PowerShell to Remove Group from List Permissions
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$ListName ="Documents"
$GroupName= "Sales Portal Members"

#Connect PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context = Get-PnPContext
 
#Get the list
$List = Get-PnPList -Identity $ListName
$Group = Get-PnPGroup -Identity $GroupName

#Break Permission Inheritance
Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments

#Remove Group from List Permissions
$List.RoleAssignments.GetByPrincipal($Group).DeleteObject()
$Context.ExecuteQuery()
SharePoint Online: Remove User or Group from List Permissions using PowerShell SharePoint Online: Remove User or Group from List Permissions using PowerShell Reviewed by Salaudeen Rajack on April 08, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.