Mar 29, 2017

PowerShell: Remove Site Collection Administrator Account

This time, I’m going to share you on the script to remove Site Collection Administrator of ALL site collections at one go.

12_13_47-000117


[Cmdletbinding()]
param (
    [string]$UserName = "",
    [string]$Tenant = "",
	[Parameter(Mandatory=$true)]
    [string]$AccountToRemove = "",
    [string]$RelativeSiteUrl = ""
)

Connect-SPOService -Url "https://$($Tenant)-admin.sharepoint.com" -Credential $UserName

if ($RelativeSiteUrl)
{
    $siteUrl = "https://$($Tenant).sharepoint.com$($RelativeSiteUrl)"
    Write-Host "Removing $AccountToRemove from $siteUrl..." -Fore Yellow -NoNewline
    Set-SPOUser -Site $site.Url -LoginName $AccountToRemove -IsSiteCollectionAdmin $false
    Write-Host "REMOVED" -Fore Green
}
else
{
    $SiteColl = Get-SPOSite
    Write-Host "Removing $AccountToRemove from:" -Fore Green
    foreach ($site in $SiteColl)
    {
        Write-Host "`t$($site.Url)..." -Fore Yellow -NoNewline
        Set-SPOUser -Site $site.Url -LoginName $AccountToRemove -IsSiteCollectionAdmin $false
        Write-Host "REMOVED" -Fore Green
    }
}

Disconnect-SPOService

Enjoy!