I would like to remove user x from all site collections and replace with user y using powershell. Does anyone have a script that works? Google hasn't been helpful this time :-(

  • is it primary or secondary site collection admin?
    – Waqas Sarwar MVP
    Commented Nov 22, 2016 at 16:29
  • I think Primary
    – Mike Blair
    Commented Nov 22, 2016 at 16:30
  • then it is easy for on prem
    – Waqas Sarwar MVP
    Commented Nov 22, 2016 at 16:35

2 Answers 2


It would be a 2 step process, this is for SPO, but could be adapted for on prem easily enough.

$sites = get-sposite -limit All 

#process sites
foreach($site in $sites){
    Set-SPOUser -LoginName "[email protected]" -IsSiteCollectionAdmin $false -Site $site.Url
    Set-SPOUser -LoginName "[email protected]" -IsSiteCollectionAdmin $true -Site $site.Url

For on prem, it would look something like (not tested, no on prem instance to verify against)

$sites = get-spsite -limit All 

    #process sites
    foreach($site in $sites){
        Set-SPUser -Identity "domain\user1" -IsSiteCollectionAdmin:$false -Web $site.RootWeb.Url # colon is necessary
        Set-SPUser -Identity "domain\user2" -IsSiteCollectionAdmin:$true -Web $site.RootWeb.Url 

You just need to set the new primary admin, it will remove the old.

 Write-Host -Foregroundcolor green "- Changing the site collection administrators"
    $site = Get-SPSite | ?{$_.Url -like "*sites/tempSite*"}
    If ($site -ne $null)
        Set-SPSite -Identity $site.Url -OwnerAlias "DOMAIN\farmacc" -SecondaryOwnerAlias "DOMAIN\svcacc" 
        Write-Host -Foregroundcolor white "- Site Collection Administrators for '$site' is changed."

I haven't tested but it will work. Changing Site Collection Administrators using PowerShell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.