none
SharePoint Online Powershell command to remove all users

    Question

  • I am using SharePoint online.  I have a need to remove all users from all security groups in a site collection (including sub-sites) that contain the word "Team".  I have found lots of bits and pieces but I'm not very experienced in power shell commands and I can't get them pieced together to work. 

    Any help would be appreciated.  


    S Boyd

    Wednesday, March 15, 2017 6:44 PM

All replies

  • Hi,

    Do you want to remove users from groups which contain the word “Team” in a specific site collections?

    If yes, you can remove these groups from the site collection using the following script:

    Connect-SPOService –Url <SharePoint Admin Center URL> –Credential <administrator email>
    
    $spGroups = Get-SPOSiteGroup -Site <site collection URL>
    
    ForEach($spGroup in $spGroups) {
    
       if($spGroup.Title -like "*Team*")
    
       {
    
         Remove-SPOSiteGroup -Site <site collection URL>-Identity $spGroup.Title
    
       }
    
    }
    

    Additional information:

    If you remove a group from a site collection, all users in this group will be removed and this group will be removed from all subsites under this site collection.

    Thanks,

    Dean Wang


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

    Thursday, March 16, 2017 9:19 AM
    Moderator
  • This one may come in handy but what I actually want to do is remove all the users within the groups that have the "Team" text as part of the name (within the single site collection but to include all the sub-sites).   Do you have a variation that will do that?

    S Boyd

    Thursday, March 16, 2017 12:41 PM
  • You can do it this way:

    $site=https://MyCompany.Sharepoint.com/sites/site1
    
    $groups=Get-SPOSiteGroup -Site $site
    $users=get-spouser -Site $site
    
    foreach($user in $users) {if($user.LoginName -match "Team"){ foreach ($group in $groups){Remove-SPOUser -Site $site -LoginName $user.loginName -Group $group.LoginName}}}

    This will remove all users that have "Team" in their loginname from ALL site groups. If such users are e.g. site administrators they will remain site administrators.

    Last line is a one-liner so that you could copy it and paste directly to the console. You can, of course, open ISE or create a .ps1 script and format it better :)

    Thursday, March 16, 2017 4:26 PM
  • The username doesn't have "Team" in it.  It's the groups that have the name "Team"  For example"

    Security group is Team 01 Members, Team 01 Members, Team 01 Owners, Team 02 Members, and so forth.  I need the script to look at all the security groups that have "Team" in it and remove all the users in each group.


    S Boyd

    Thursday, March 16, 2017 4:42 PM
  • Do you want to do it in Site Groups or Security groups?

    Site groups are used only in SharePoint. Security groups can be used in all technologies for variety of purposes, not only SharePoint permissions.

    In Powershell the difference is between Get-SPOSiteGroup and Get-MsolGroup.

    Friday, March 17, 2017 9:48 AM
  • SharePoint site groups. 

    S Boyd

    Friday, March 17, 2017 12:27 PM
  • Then try this one:

    $groups=Get-SPOSiteGroup -Site $site | where {$_.Title -match "Teams"}
    
    foreach($group in $groups) {$groupUsers=Get-SPOUser -Site $site -Group $group.Title; foreach($user in $groupUsers){ Remove-SPOUser -Site $site -LoginName $user.LoginName -Group $group.Title}}

    First verify on a test site collection whether the results correspond to what you wanted.

    Monday, March 20, 2017 8:01 AM
  • Hi,

    You can run the following script, which removes all the users with all “Team” groups from site collection and sub sites:

    Connect-SPOService –Url <SharePoint Admin Center URL> –Credential <administrator email>
    
    $Site = "<site collection URL>"
    
    $spGroups = Get-SPOSiteGroup -Site $Site
    
    ForEach($spGroup in $spGroups) {
    
       if($spGroup.Title -like "*Team*")
    
       {
    
           $Users=Get-SPOUser -Site $site -Group $spGroup.Title
    
           foreach($User in $Users){ 
    
              Remove-SPOUser -Site $site -LoginName $User.LoginName -Group $spGroup.Title
    
           }
    
       }
    
    }
    

    Thanks,

    Dean Wang


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

    Monday, March 20, 2017 1:04 PM
    Moderator
  • This is working at the site collection level.  I have multiple team sites under one subsite and sometimes I only need to remove all the users in all the team sites under that one subsite. 

    S Boyd

    Wednesday, March 22, 2017 12:38 PM
  • Hi,

    Groups live in site collection level in SharePoint. It means that if you remove a group or users under a group, they will be removed from all sub sites dynamically.

    Thanks,

    Dean Wang


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

    Monday, March 27, 2017 8:45 AM
    Moderator
  • This is working.  I've remove all the users in a "*Fall Visitors" group.  Now I need to remove all the users in the team sites.  Will the script below work.  The teams are call "Instruction (and it changes) Fall Team 01 (changes to 02, 03, ect) Members.  

    I need to clean out all the users in the members group for each instructors Fall Team sites.

    if($spGroup.Title -like "*Fall Team*Members")

    


    S Boyd

    Monday, March 27, 2017 5:02 PM
  • I got the format correct for removing the users using 

    *Fall Team*Members.

    Thank you for your help.


    S Boyd

    Friday, March 31, 2017 7:08 PM