How to remove SCA users from Site Collection Admins?


This is kind of similar requirement from my previous blog article.

How to remove users from multiple Azure AD Groups?

But in this article I will provide only the code for removing the Site collection Admins from SharePoint Site. The steps for running this code is same as the above article.

$Users2Remove = (

$ClientId = "7c244c08-9875-4ffe-b39d-34f9b6853f6b"
$Tenant = "" # replace your tenant name

Import-Csv C:\Contoso\teamSites.CSV | 
    ForEach-Object {
            $newSiteUrl = $_.SiteURL
            $newSiteConn = Connect-PnPOnline -ClientId $ClientId -Url $newSiteUrl -Tenant $tenant -CertificatePath 'MyPnPApplication.pfx' -ReturnConnection

            # Get the site collection administrators  
            $scaColl = Get-PnPSiteCollectionAdmin -Connection $newSiteConn
            foreach($sca in $scaColl)  
                Write-Host $sca.LoginName
                $modifiedLoginName = $sca.LoginName
                $modifiedLoginName = $modifiedLoginName -replace "i:0#.f\|membership\|", ""
                if ( ( $Users2Remove -contains  $modifiedLoginName ) -eq $true )
                    Write-Host $("Remove {0}" -f $sca.LoginName)
                    Remove-PnPSiteCollectionAdmin -Owners $sca.LoginName
                    Write-Host $("Removed..")
            $ErrorMessage = $_.Exception |  Out-String
            Write-Host $("Exception {0}" -f $ErrorMessage);
            Write-Host $("Disconnecting to {0}" -f $newSiteUrl);
            Disconnect-PnPOnline -Connection $newSiteConn
            Write-Host $("Disconnected to {0}" -f $newSiteUrl);

About Pankaj

I am a Developer and my linked profile is
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s