Skip to 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
Write-Host $m365Status
if ($m365Status -eq "Logged Out") {
  # Connection to Microsoft 365
  m365 login
  $m365Status = m365 status

m365 spo user remove --webUrl $webUrl --loginName "i:0#.f|membership|$userToRemove" --confirm
m365 spo site classic set --url $webUrl --owners $userToAdd


  • SharePoint Online
  • Governance