To interact with SharePoint Online data using PowerShell, you’ll need to obtain and import the SharePoint Online Module. First, ensure that the SharePoint Online Management Shell is installed. If it’s not already installed, you should proceed to obtain it.
You can download and install it from the SharePoint Online Management Shell page.
You can launch it once it is installed on your PC and below is how you check the version
Step 1
Connect to your SharePoint Online tenant by typing “Connect-SPOservice” and then typing the address of your admin site once prompted for URL.
Connect-SPOService Url: https://test-admin.sharepoint.com
Step 2
Get the list of all sites: You can use the Get-SPOSite cmdlet to retrieve a list of all sites in your SharePoint Online environment
Get-SPOSite -Limit ALL
This script b(below) will loop through all the sites, retrieve their administrators, and store the information in a custom object. It then exports this data to a CSV file named “SiteAdmins.csv” in the current directory. You can change the file path and name to your preference.
Please ensure that you have the necessary permissions to run these commands, as access to site administrators’ information might be restricted, and you should be a SharePoint administrator to access this data. Also, note that this information might change over time as site administrators are added or removed.
$adminList = @() foreach ($site in $sites) { $siteUrl = $site.Url $siteAdmins = Get-SPOSite -Identity $siteUrl | Select-Object -ExpandProperty Owner # Create a custom object for each site and its administrators $siteInfo = [PSCustomObject]@{ "Site URL" = $siteUrl "Site Administrators" = $siteAdmins } # Add the custom object to the adminList array $adminList += $siteInfo } # Export the adminList array to a CSV file $adminList | Export-Csv -Path "SiteAdmins.csv" -NoTypeInformation