0

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 :-(

1

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 "user.1@domain.com" -IsSiteCollectionAdmin $false -Site $site.Url
    Set-SPOUser -LoginName "user.2@domain.com" -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 
    }
0

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, privacy policy and cookie policy

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