none
Remove all users from a group RRS feed

  • Question

  • Hi,

    I have a small snippet of CSOM PowerShell code to clear a group before adding users. I keep getting an error "The collection was modified. Enumeration operation may not execute." Any ideas what I might be doing wrong ?

    #Getting the specific SharePoint Group where we want to add the user
        $GroupName = "Team " + $item["Title"] + " Members"
        $Group = $Groups.GetByName($GroupName)
        $ctx.Load($Group)
        $ctx.ExecuteQuery()
    
    #Clear all users from the group before adding them
        $GroupUsers = $group.Users
        $ctx.Load($groupUsers)
        $ctx.ExecuteQuery()
    
        Foreach ($user in $groupusers)
        {
            $groupusers.Remove($user)
            $groupusers.Update
            $web.Update()
            $ctx.ExecuteQuery()
        }


    Stephane

    Monday, March 13, 2017 4:20 PM

Answers

  • Finally, I used the following code:

    $Groups=$Web.SiteGroups 
    $ctx.Load($Groups)
    $ctx.ExecuteQuery()
             
    #Getting the specific SharePoint Group where we want to add users
    
    $GroupName = "Team " + $web.Title + " Members"
    $Group = $Groups.GetByName($GroupName)
    $ctx.Load($Group)
    $ctx.ExecuteQuery()
    
    #Clear all users from the group before adding them
    $GroupUsers = $group.Users
    $ctx.Load($groupUsers)
    $ctx.ExecuteQuery()
    while ($groupusers.Count)
    {
        $groupusers.Remove($Groupusers[0])
    }
    
    

    Thanks all for your suggestions


    Stephane

    • Marked as answer by S Bouillon Tuesday, March 14, 2017 8:49 AM
    Tuesday, March 14, 2017 8:49 AM

All replies

  • Hi

    Here you go

    https://blogs.msdn.microsoft.com/spses/2011/11/09/powershell-script-to-remove-all-users-from-a-sharepoint-group/


    Please remember to click Mark as Answer on the answer if it helps you

    Monday, March 13, 2017 4:33 PM
  • Thanks for your time and effort,

    That script does not work. I should have mentioned this is on a O365 SharePoint online environment.


    Stephane

    Monday, March 13, 2017 9:01 PM
  • Hi,

    this's not a SP problem but a code issue.
    You cannot invoke $groupusers.remove() when enumerate it. As remove() function will modify the user collection($groupusers).
    You can push all users into a list, then enumerate this list to delete them from $groupusers.

    --Jeff

    Tuesday, March 14, 2017 3:35 AM
  • Hi Stephane,

    The following PowerShell script for your reference:

    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 
    
    $siteURL = "https://xxx.sharepoint.com/sites/dennis"
    $groupName = "Dennis Members"
    
    $username = "dennis@xxx.onmicrosoft.com"
    $Password = "xxxx"
    $password = ConvertTo-SecureString $Password -AsPlainText -Force 
    
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
    
    $group = $ctx.Site.RootWeb.SiteGroups.GetByName($groupName);
    $ctx.Load($group);
    $ctx.ExecuteQuery();
    
    #Clear all users from the group before adding them
    $groupUsers = $group.Users
    $ctx.Load($groupUsers)
    $ctx.ExecuteQuery()
    
    $usersToDelete=@()
    
    foreach($user in $groupUsers)
    {
    	$usersToDelete+=$user
    }
    foreach($userToDelete in $usersToDelete)
    {
    	$groupUsers.Remove($userToDelete)
    	$ctx.ExecuteQuery()	
    }
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Tuesday, March 14, 2017 8:30 AM
    Moderator
  • Finally, I used the following code:

    $Groups=$Web.SiteGroups 
    $ctx.Load($Groups)
    $ctx.ExecuteQuery()
             
    #Getting the specific SharePoint Group where we want to add users
    
    $GroupName = "Team " + $web.Title + " Members"
    $Group = $Groups.GetByName($GroupName)
    $ctx.Load($Group)
    $ctx.ExecuteQuery()
    
    #Clear all users from the group before adding them
    $GroupUsers = $group.Users
    $ctx.Load($groupUsers)
    $ctx.ExecuteQuery()
    while ($groupusers.Count)
    {
        $groupusers.Remove($Groupusers[0])
    }
    
    

    Thanks all for your suggestions


    Stephane

    • Marked as answer by S Bouillon Tuesday, March 14, 2017 8:49 AM
    Tuesday, March 14, 2017 8:49 AM
  • Hi Stephane,

    Did you check my PowerShell script above? It works in my test environment.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 14, 2017 8:54 AM
    Moderator
  • I got an error that GroupUsers does not have an Update method

    Stephane

    Tuesday, March 14, 2017 9:21 AM
  • Hi,

    I modify the code above and it also works.

    $usersToDelete=@()
    
    foreach($user in $groupUsers)
    {
    	$usersToDelete+=$user
    }
    foreach($userToDelete in $usersToDelete)
    {
    	$groupUsers.Remove($userToDelete)
    	$ctx.ExecuteQuery()	
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 14, 2017 11:08 AM
    Moderator