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– Waqas Sarwar MVP ♦2016-11-22 16:29:30 +00:00Commented Nov 22, 2016 at 16:29
- I think PrimaryMike Blair– Mike Blair2016-11-22 16:30:40 +00:00Commented Nov 22, 2016 at 16:30
- then it is easy for on premWaqas Sarwar MVP– Waqas Sarwar MVP ♦2016-11-22 16:35:34 +00:00Commented Nov 22, 2016 at 16:35
Add a comment |
2 Answers
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