Jun 10 2020 03:59 AM
Hi,
I'm looking for a Powershell to remove account from Additional admins on all SharePoint sites.
Anyone have the powershell script to do that?
Jun 10 2020 07:53 AM
@elaheh : Yes you can do that using PowerShell
1) First, connection to the SharePoint using Connect-SPO service.
2) Get handle of all site collections using GetSPOSite.
3) Loop through each site
4) Get all site collection admins using method Get-SPOUser and IsSiteAdmin = true.
5) Loop through each admin. (This will give you all the admins name).
6) Inside this loop, add If condition for the admin whom you don't want to remove.
7) And in Else condition write method for to remove. here you have to set IsSiteCollectionAdmin == False.
I did this code long time back, it will be somewhere in the archival, need to check for the code.
By that time, please check if you can achieve this through above guidelines.
Hope this helps !
Jun 10 2020 11:19 PM
Thanks @Ashish_Kohale
I will try.
Jun 11 2020 07:00 AM
@Ashish_Kohale I would appreciate it if you find the code. :) thanks
Jun 12 2020 01:59 AM
@elaheh : Below is the code( not exactly similar to what I wrote but you can refer and manipulate as per your need)
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking
#Variables for processing
$AdminURL = "https://<SharePointTenantName>-admin.sharepoint.com/"
$AdminAccount="adminAccount@email.com"
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential (Get-Credential)
#Get All Site Collections
$Sites = Get-SPOSite -Limit ALL
#Loop through each site and add site collection admin
Foreach ($Site in $Sites)
{
Write-host "Scanning site:"$Site.Url -f Yellow
#Get All Site Collection Administrators
$Admins = Get-SPOUser -Site $site.Url | Where {$_.IsSiteAdmin -eq $true}
#Iterate through each admin
Foreach($Admin in $Admins)
{
#Check if the Admin Name matches
If($Admin.LoginName -eq $AdminAccount)
{
#Remove Site collection Administrator
Write-host "Removing Site Collection Admin from:"$Site.URL -f Green
Set-SPOUser -site $Site -LoginName $AdminAccount -IsSiteCollectionAdmin $False
}
}
}
Hope It helps !
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Jun 16 2020 03:37 AM
Jun 16 2020 03:45 AM
@elaheh : That's great news. Happy coding !!!.
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.