Skip to main content

Replace site collection admin with another user

Author: Patrick Lamber Inspired By: Salaudeen Rajack

The script removes a user from a site collection and adds a new one as site collection admin.

$userToAdd = "<upnOfUserToAdd>"
$userToRemove = "<upnOfUserToRemove>"
$webUrl = "<spoUrl>"

$m365Status = m365 status --output text
Write-Host $m365Status
if ($m365Status -eq "Logged Out") {
# Connection to Microsoft 365
m365 login
}

m365 spo user remove --webUrl $webUrl --loginName "i:0#.f|membership|$userToRemove" --force
m365 spo site set --url $webUrl --owners $userToAdd
CTRL + M