Replace site collection admin with another user
Summary
The script removes a user from a site collection and adds a new one as site collection admin.
Warning
Please be aware this script contains a command that will remove or delete an artifact, ensure you test and understand the implications of running the script.
$userToAdd = "<upnOfUserToAdd>"
$userToRemove = "<upnOfUserToRemove>"
$webUrl = "<spoUrl>"
$m365Status = m365 status
Write-Host $m365Status
if ($m365Status -match "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
Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365
Source Credit
Sample first appeared on Replace site collection admin with another user | CLI for Microsoft 365
Contributors
Author(s) |
---|
Patrick Lamber |
Inspired By Salaudeen Rajack |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.