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.