Patrick Lamber
Patrick Lamber Microsoft Office Development MVP

How do I remove a site collection admin from all site collections in SharePoint Online?

I had the requirement to remove a site collection admin assigned to all SharePoint Online site collections with a script. To achieve this goal I was using Pnp PowerShell. The script was executed by an account having permissions to all SharePoint Online sites.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$username = "putYourUserNameHere"
$password = "putYourPasswordHere"
$tenant = "putYourTenantHere"
# alternatively use the loginname. ensure you are going to change the Get-PnpUser filter
$displayName = "*putTheDisplayNameOfTheUserHere*"

[SecureString]$SecurePass = ConvertTo-SecureString $password -AsPlainText -Force 
$creds = New-Object System.Management.Automation.PSCredential($username, $SecurePass) 

Connect-PnPOnline "https://$tenant-admin.sharepoint.com" -Credentials $creds

Get-PnPTenantSite | ForEach-Object {
   $site = $_
   Write-Host "Processing $($site.Url)..."
   Connect-PnPOnline $site.Url -Credentials $creds
   Get-PnPUser | ? Title -like $displayName | Remove-PnPSiteCollectionAdmin
}

comments powered by Disqus