Wednesday, January 6, 2016

SharePoint Online: Remove User from Group using PowerShell

Removing users from SharePoint online security groups can be done in just few clicks.  Follow these steps to remove users from a group.

SharePoint Online - How to Remove a User from Group:
To delete users from a SharePoint online group,

  • Navigate to your SharePoint online site, Click on Site Settings gear >> Click on Site Settings from the menu.
  • From the Site Settings page, Click on "People and groups" under Users And Permissions section.
  • On the Left navigation menu, select the group from which you'd like to remove the users.
  • Tick the check box next to the user(s) you want to remove.
  • Click the Actions drop-down arrow >> Select Remove Users from Group.
  • In the confirmation pop-up message box, Click OK to confirm the deletion.
    sharepoint online remove user from group
If you don't see the Actions menu, it's because you don't have permissions to edit members of that Group!

SharePoint Online: Remove User from Group using PowerShell:
Lets use PowerShell to delete users from a SharePoint online group.
#Load SharePoint CSOM 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"

Function Remove-UserFromGroup()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $GroupName,
        [Parameter(Mandatory=$true)] [string] $UserID
    )
   Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the User
        $User=$Ctx.web.EnsureUser($UserID)
        $Ctx.Load($User)
        #Get the Group
        $Group=$Ctx.web.SiteGroups.GetByName($GroupName)
        $Ctx.Load($Group)
        $Ctx.ExecuteQuery()

        #Check if user member of the group
        $IsMember = $False
        $GroupUsers = $Group.Users 
        $Ctx.Load($GroupUsers)
        $Ctx.ExecuteQuery()
        Foreach($GrpUser in $GroupUsers){
            if($GrpUser.id -eq $User.Id)
            {
                $IsMember = $True
            }
        }
        if($IsMember -eq $False)
        {
            Write-host "User Doesn't Exists in the Group!" -ForegroundColor Yellow
        }
        else
        {
            #Remove user from the group
            $Group.Users.RemoveByLoginName($User.LoginName)
            $Ctx.ExecuteQuery()
            Write-host "User Removed from the Group Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Removing User from Group!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL = "https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$UserID="DavePrudenti@crescent.com"

#Call the function to remove user from group
Remove-UserFromGroup -SiteURL $SiteURL -GroupName $GroupName -UserID $UserID 


Remove User from Group using SharePoint Online Management Shell
We can also use Remove-SPOUser cmdlet to remove a user from specific group.
#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$LoginName="Salaudeen@crescent.com"

#Get Credentials to connect
$Credentials = Get-Credential
Connect-SPOService -url $AdminSiteURL -credential $Credential

#Remove user from group
Remove-SPOUser -LoginName $LoginName -Site $SiteURL -Group $GroupName
powershell to remove user from group sharepoint online

Remove User from All Groups in a site:
#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$LoginName="Salaudeen@crescent.com"

#Get Credentials to connect
$Credentials = Get-Credential
Connect-SPOService -url $AdminSiteURL -credential $Credential

#Get all groups  
$Groups= Get-SPOSiteGroup -Site $SiteURL

#Remove user from each group 
Foreach($Group in $Groups)
{
   Remove-SPOUser -Site $SiteURL -LoginName $LoginName -Group $Group.Name
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...