SharePoint / Powershell remove multiple groups from sites

Evan Emmanouilidis 21 Reputation points
2022-07-04T10:49:30.107+00:00

Hi,

I have around 20 sites in SharePoint that were created and have there respective Owner, Members and visitors permission groups however i would like to bulk remove these via Powershell and add M365 Groups which have users in ( mOwner and mMember)

mOwner permission group would have full access.
mMember would just have edit.

Does anyone know if there is a way to do this? i can do it manually but will take a while to do and so would like to be able to do it using Powershell and importing URL's from a CSV.

Kind Regards

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,167 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,620 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 38,576 Reputation points Microsoft Vendor
    2022-07-05T07:22:42.397+00:00

    Hi @Evan Emmanouilidis ,
    Per my test, you can refer to following code to keep the groups you want, and remove those you need to delete

    $csv = Import-Csv -Path c:\temp\test.csv  
    $siteurls = $csv.siteurl  
      
    ForEach($siteUrl in $siteurls){  
    	$keep = 'Group 1', 'Group 2'  
    	Connect-SPOService -Url 'https://test-admin.sharepoint.com' #tenant admin URL  
    	$site = Get-SPOSite $siteUrl  
    	Get-SPOSiteGroup -Site $site | ? LoginName -NotIn $keep |  
     	 % { Set-SPOSiteGroup -Site $site -Identity $_.LoginName -PermissionLevelsToRemove $_.Roles }    
    }  
    

    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.



0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.