Share via

sharepoint sites

Glenn Maxwell 13,761 Reputation points
2025-04-06T22:54:17.8733333+00:00

Hi All,

I am using SharePoint online, I have users list in a CSV file in the format shown below. I would like to import this CSV file and retrieve all the SharePoint sites where these users are listed as owners. Along with the owners, i want to fetch Date Created and Date it was last updated. I want to include both Microsoft 365 group-connected sites and non-group-connected sites in the results.( i have limited knowledge in sharepoint, i want to fetch hub sites, communication sites and if any other sites in sharepoint these users are owners of).

Below is my csv format
users
******@contoso.com
******@contoso.com


i want to try something like the below but i have not tested it please guide me.

# Variables for Admin Center
$AdminCenterURL = "https://contoso-admin.sharepoint.com"
$CSVPath = "C:\Temp\input.csv"
# Import CSV
$UserList = Import-Csv -Path $CSVPath
# Get Credentials to connect
$Cred = Get-Credential
# Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminCenterURL -Credential $Cred
# Connect to Azure AD
Connect-AzureAD -Credential $Cred
# Get all Site Collections
$Sites = Get-SPOSite -Limit ALL
$SiteOwners = @()
# Get Site Owners for each site collection
foreach ($Site in $Sites) {
    $Owners = @()
    if ($Site.Template -like 'GROUP*') {
        # Group-connected site - get GroupID from site
        try {
            $GroupOwners = Get-AzureADGroupOwner -ObjectId $Site.GroupId | Select-Object -ExpandProperty UserPrincipalName
            $Owners += $GroupOwners
        }
        catch {
            Write-Warning "Unable to retrieve group owners for site: $($Site.Url)"
        }
    }
    else {
        # Classic site - use Owner property
        if ($Site.Owner) {
            $Owners += $Site.Owner
        }
    }
    # Check if any of the CSV users are owners
    $MatchedUsers = $UserList.UserPrincipalName | Where-Object { $Owners -contains $_ }
    if ($MatchedUsers.Count -gt 0) {
        $SiteOwners += [PSCustomObject]@{
            'Site Title' = $Site.Title
            'URL'        = $Site.Url
            'Owner(s)'   = ($Owners -join "; ")
            'Matched User(s)' = ($MatchedUsers -join "; ")
        }
    }
}
# Output results
$SiteOwners
# Export Site Owners report to CSV
$SiteOwners | Export-Csv -Path "C:\Temp\SiteOwnersReport.csv" -NoTypeInformation
            'Matched User(s)' = ($MatchedUsers -join "; ")
        }
    }
}
# Output results
$SiteOwners
# Export Site Owners report to CSV
$SiteOwners | Export-Csv -Path "C:\Temp\SiteOwnersReport.csv" -NoTypeInformation


Microsoft 365 and Office | SharePoint | Development
0 comments No comments

1 answer

Sort by: Most helpful
  1. Xyza Xue_MSFT 30,256 Reputation points Microsoft External Staff
    2025-04-08T06:11:52.54+00:00

    To achieve your goal of importing a CSV file containing user email addresses, identifying SharePoint sites where these users are listed as owners, and retrieving details such as the site's creation date and last updated date, you can utilize PowerShell along with the SharePoint Online Management Shell and PnP PowerShell modules. Below is a step-by-step guide tailored to your requirements:

    Prerequisites:

    1.Install SharePoint Online Management Shell:

    • This module allows you to manage SharePoint Online settings.​
    • Installation:
      • Open PowerShell as an administrator.
      • Run: Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force -AllowClobber

    2.Install PnP PowerShell Module:

    PnP PowerShell provides cmdlets for SharePoint Online operations.​SharePoint Diary+2SharePoint Diary+2PowerShell FAQs+2

    Installation:

    Run: Install-Module -Name PnP.PowerShell -Force -AllowClobber

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.