Hi @Ameer ,
Please try this:
# Connect to SharePoint Online
Connect-SPOService -Url https://mydoamin-admin.sharepoint.com
# Create an empty array to store site information
$siteInfo = @()
# Get all site collections
$siteCollections = Get-SPOSite -Limit All
# Loop through each site collection
foreach ($siteCollection in $siteCollections) {
$siteCollectionUrl = $siteCollection.Url
Write-Host "Site Collection URL: $siteCollectionUrl"
# Get all sites within the site collection
$sites = Get-SPOSite -Limit All -Filter "Url -like '$siteCollectionUrl*'"
# Loop through each site
foreach ($site in $sites) {
$siteUrl = $site.Url
Write-Host "Site URL: $siteUrl"
# Get site members
$members = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Members" }
$memberUsers = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Members" } | Select-Object -ExpandProperty Users
# Add members to site information array
foreach ($member in $memberUsers) {
$siteInfo += [PSCustomObject]@{
'Site Collection URL' = $siteCollectionUrl
'Site URL' = $siteUrl
'Members' = $member.LoginName
}
}
# Get site owners
$owners = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Owners" }
$ownerUsers = Get-SPOSiteGroup -Site $siteUrl | Where-Object { $_.Title -eq "Owners" } | Select-Object -ExpandProperty Users
# Add owners to site information array
foreach ($owner in $ownerUsers) {
$siteInfo += [PSCustomObject]@{
'Site Collection URL' = $siteCollectionUrl
'Site URL' = $siteUrl
'Owners' = $owner.LoginName
}
}
}
}
# Display site information
$siteInfo | Format-Table -AutoSize
# Export site information to CSV files for members and owners
$siteInfo | Where-Object { $_.Members -ne $null } | Select-Object 'Site Collection URL', 'Site URL', 'Members', 'Permissions' | Export-Csv -Path ".\SharePoint\SharePoint_Members.csv" -NoTypeInformation -Encoding UTF8
$siteInfo | Where-Object { $_.Owners -ne $null } | Select-Object 'Site Collection URL', 'Site URL', 'Owners', 'Permissions' | Export-Csv -Path ".\SharePoint\SharePoint_Owners.csv" -NoTypeInformation -Encoding UTF8
This modified script uses the Get-SPOSiteGroup
cmdlet to retrieve the "Members" and "Owners" groups for each site, and then uses the Select-Object
cmdlet with the -ExpandProperty
parameter to retrieve the users in those groups. It then adds the user to the site information array.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.