For SharePoint Online: What PowerShell can provide a list of ALL site collections along with their owners and number of visits in the last 90 days?

frob 4,261 Reputation points
2023-06-07T15:11:47.5733333+00:00

Hi there

For SharePoint Online:

What PowerShell can output a CSV file with a list of ALL site collections in our SP Online along with:

  • Site collection owners
  • Number of visits in the last 90 days

Thank you.

Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,781 Reputation points
    2023-06-08T10:20:34.07+00:00
    Hello Frob,
    
    Thank you for your question and for reaching out with your question today.
    
    To retrieve a CSV file with a list of all site collections in SharePoint Online along with site collection owners and the number of visits in the last 90 days, you can use the SharePoint Online Management Shell module and PowerShell. Here's an example PowerShell script:
    
    # Connect to SharePoint Online
    Connect-SPOService -Url "
    
    # Get all site collections
    $siteCollections = Get-SPOSite
    
    # Create an array to store site collection data
    $siteCollectionData = @()
    
    # Iterate through each site collection
    foreach ($siteCollection in $siteCollections) {
        # Get site collection owners
        $owners = Get-SPOUser -Site $siteCollection.Url -Role "Full Control" | Select-Object -ExpandProperty LoginName
    
        # Get number of visits in the last 90 days
        $analytics = Get-SPOAnalyticsSiteUsageData -SiteUrl $siteCollection.Url -StartDate (Get-Date).AddDays(-90) -EndDate (Get-Date)
        $visits = $analytics.Usage | Measure-Object -Property Views -Sum | Select-Object -ExpandProperty Sum
    
        # Create an object with site collection data
        $siteCollectionData += [PSCustomObject]@{
            "Site Collection URL" = $siteCollection.Url
            "Site Collection Owners" = $owners -join ";"
            "Number of Visits (Last 90 Days)" = $visits
        }
    }
    
    # Export the site collection data to a CSV file
    $siteCollectionData | Export-Csv -Path "C:\Path\to\output.csv" -
    
    Make sure to replace `"
    
    When you run the script, it will connect to SharePoint Online, retrieve information for each site collection, and save the data to a CSV file with the specified columns: Site Collection URL, Site Collection Owners, and Number of Visits (Last 90 Days).
    
    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.
    
    If the reply was helpful, please don’t forget to upvote or accept as answer.
    
    Best regards.
    
    0 comments No comments

Your answer

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