We used PowerShell for removing users from SCA (Site Collection Administrator). You have to add all sites in .CSV file (mention full path of .csv in $csvPath). PowerShell code will read the .csv file and it will one by one connect to all sites using PnP. You have to add all users in the $allEmailIDs array. $allErrorSites will print all error sites.
The error will be,
- If you do not have access to that site.
- If the email id is not valid.
Use below code in PowerShell,
- $csvPath = ".CSV FILE PATH"
- $allSitesUrls = Get - Content $csvPath
- $allEmailIDs = @("EMAIL ID - 1", "EMAIL ID - 2")
- $allErrorSites = @()
- try {
- ForEach($singleSiteUrl in $allSitesUrls) {
- : InnerLoop ForEach($emailID in $allEmailIDs) {
- try {
- Write - Host "Start Process for : $($singleSiteUrl)";
- Connect - PnPOnline $singleSiteUrl - UseWebLogin
- Remove - PnPSiteCollectionAdmin - Owners $emailID
- Write - Host "SCA access has been removed of : $($emailID) from site $($singleSiteUrl)";
- } catch {
- #Add the object with property to an Array
- $allErrorSites += $singleSiteUrl;
- [string] $FuncName = $MyInvocation.MyCommand.Name;
- Write - Host - Message $_ - FunctionName $FuncName;
- }
- }
- }
- #It will print all sites which got error
- Write - host - f Red $allErrorSites;
- } catch {
- [string] $FuncName = $MyInvocation.MyCommand.Name;
- Write - Host - Message $_ - FunctionName $FuncName;
- }