Forum Discussion
Bhavpreet Bains
Sep 28, 2018Iron Contributor
List all Site Collection Admins Powershell
Hello, I am trying to get all site collection admin for the sites in the tenant using Powershell. However, the cmdlet Get-SPOSite fetches only the primary site collection admins. The only way to ...
Malgorzata_SITS
Jun 27, 2020Copper Contributor
Alex Carlock did you manage to get it work the way you wanted? I'm struggling with the same thing now and **bleep**, this is such a simple and basic thing that it must be somehow available...
Alex Carlock
Jun 27, 2020Iron Contributor
Malgorzata_SITS, I ended up doing what I suggested above. That was the only workaround I could come up with.
Here's the last powershell script I ended up with (It was quick and dirty, so no warranties, use at your own risk, etc.) You'll need to update "domain" to be your own.
$username = "$env:username@domain.com"
$Sites = Get-SPOSite -IncludePersonalSite $true -Limit all -Filter "Url -like 'domain-my.sharepoint.com/personal/'"
foreach ($Site in $sites) {
$URL = $Site.URL
$Admins = Get-SPOUser -Site $URL -Limit all | Where IsSiteAdmin -eq $True | Select-Object @{Label="Site";Expression={"$URL"}},@{Label="AdminName";Expression={$_.DisplayName}}, @{Label="AdminLogin";Expression={$_.LoginName}}, @{Label="RemoveNonOwnerAdmin";Expression={If ($URL.replace("https://domain-my.sharepoint.com/personal/","") -ne $_.LoginName.replace("@","_").replace(".","_")) {"Set-SPOUser -Site $URL -LoginName $($_.LoginName) -IsSiteCollectionAdmin `$False"} else {""}}}
if ($Admins) {
$Admins | export-csv c:\temp\OneDriveAdmins.csv -NoTypeInformation -append -encoding ASCII
} else {
Set-SPOUser -Site $URL -LoginName $username -IsSiteCollectionAdmin $true
Get-SPOUser -Site $URL -Limit all | Where {$_.IsSiteAdmin -eq $True -and $_.LoginName -ne $username} | Select-Object @{Label="Site";Expression={"$URL"}},@{Label="AdminName";Expression={$_.DisplayName}}, @{Label="AdminLogin";Expression={$_.LoginName}}, @{Label="RemoveNonOwnerAdmin";Expression={If ($URL.replace("https://domain-my.sharepoint.com/personal/","") -ne $_.LoginName.replace("@","_").replace(".","_")) {"Set-SPOUser -Site $URL -LoginName $($_.LoginName) -IsSiteCollectionAdmin `$False"} else {""}}} | export-csv c:\temp\OneDriveAdmins.csv -NoTypeInformation -append -encoding ASCII
Set-SPOUser -Site $URL -LoginName $username -IsSiteCollectionAdmin $false
}
}