Forum Discussion
List all Site Collection Admins Powershell
Can you try this script and let me know.
$Creds = Get-Credential $site = ‘https://tenant-admin.sharepoint.com’ Connect-SPOService -Url $site -Credential $Creds $AllSites= Get-SPOSite -Limit All $users = @(); foreach ($Allsite in $AllSites) { $AllUsers = Get-SPOUser -Site $AllSite.Url -Limit all | select DisplayName, LoginName,IsSiteAdmin $users+=$AllUsers $AllUsers = $null #Write-Host $AllSite.Url" completed" } $users | Export-Csv -Path "C:\Users\Desktop\allusers.csv" -NoTypeInformation -Force $Data = Import-Csv "C:\Users\Desktop\allusers.csv" foreach($aUser in $Data) { if($aUser.IsSiteAdmin -eq “True”) { Write-Host $aUser.DisplayName $aUser.LoginName } }
To run cmdlet Get-SPOUser, you must be a SharePoint Online global administrator and a site collection administrator. I am looking for something for which I don't have to grant site collection admin access to my account.
Since the secondary admins are available from SharePoint Admin console, the admin account should atleast be able to get them.
Also, we can grant a user site collection admin (using powershell) without granting site collection admin access to the admin account. So, I think there should be a way to fetch them without granting site collection admin access to the admin account.
- bmartin921Jun 10, 2019Copper Contributor
These commands can be run with Sharepoint Administrator rights and do not require SiteCollectionAdmin permissions to the site you're modifying. Do not run this as a script -- this is a reference document for you to be able to implement functionality into your application. Use at your own risk and use context to understand what the commands do before you run them.
# ONEDRIVE SITE MANAGEMENT
# Assuming you have the SPO/PnP modules installed from MS already, found here:
# https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-online/connect-sharepoint-online?view=sharepoint-ps
# https://www.microsoft.com/en-us/download/details.aspx?id=35588
# Uncomment the "Install-Module"s if you haven't already installed the modules in PS after installing them to your machine using the links above.
# Install-Module -Name Microsoft.Online.SharePoint.PowerShell -ErrorAction Stop
Import-Module -Name Microsoft.Online.SharePoint.PowerShell
# Install-Module SharePointPnPPowerShellOnline -ErrorAction Stop
Import-Module SharePointPnPPowerShellOnline
$365cred = (Get-Credential)
# Get PersonalUrl of a OneDrive site
$upn = 'john.doe@domain.com'
$tenantName = 'domain-admin'
Connect-PnPOnline -Url "https://$($tenantname).sharepoint.com" -Credentials $365Cred
$url = (Get-PnPUserProfileProperty -Account $upn).PersonalUrl
Disconnect-PnPOnline
# or
# Manual override if siteUrl known
# $url = "https://$($tenantname).sharepoint.com/personal/john_doe_domain_com"
# Get information about a OneDrive site incl. SiteCollectionAdmins
# (requires personalUrl)
Connect-SPOService -URL "https://$($tenantname).sharepoint.com" -Credential $365Cred
Get-SPOSite -Identity $url -Detailed | Format-List
Get-SPOUser -Site $url -Limit all | Select-Object DisplayName, LoginName, IsSiteAdmin | Sort-Object IsSiteAdmin, DisplayName | Format-Table -GroupBy IsSiteAdmin -AutoSize
# Add a user to site owner access
Set-SPOUser -Site $url -LoginName "upnOfUserToAdd" -IsSiteCollectionAdmin $true
# Remove a user from site owner access
Set-SPOUser -Site $url -LoginName "upnOfUserToRemove" -IsSiteCollectionAdmin $false- Alex CarlockJan 30, 2020Iron Contributor
bmartin921, Thanks for the details and the thoughts. Unfortunately, I'm in the same boat as @Bhavpreet Bains. The commands work to add and remove Site Collection Admins even if you're not a site collection admin, but they cannot VIEW the existing site collection admins unless you are also a site collection admin.
I'm surprised Microsoft hasn't updated the commands to allow that functionality for Office 365 Global Admins and SharePoint Admins. Another option is for MSFT to build in such an audit report into the Office 365 or SharePoint admin sites.
My next option is to write a script that does the following:
- Pull the list of admins
- If I get results, great
- If I get no results add myself as an admin
- Pull the list of admins
- Remove my admin access
It's messy, but it should do the trick.
- CissongDec 10, 2021Brass Contributor
Hello Alex Carlock were you able to get a script to get all the sites with site admins