Create CSV file of all sites in a particular site collection in SharePoint online

Alan Day 21 Reputation points
2022-05-23T09:03:19.797+00:00

I need to create a CSV list of all sites created under site collection .../sites/workspaces in our SharePoint Online tenancy. Is there a way to do this, preferably through either PowerShell or SharePoint Online Management Shell? I have tried numerous scripts that I have found through different forums without success.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,748 questions
OneDrive Management
OneDrive Management
OneDrive: A Microsoft file hosting and synchronization service.Management: The act or process of organizing, handling, directing or controlling something.
1,139 questions
{count} votes

Accepted answer
  1. Yi Lu_MSFT 17,461 Reputation points
    2022-05-24T07:11:33.667+00:00

    Hi @Alan Day
    You could use this script:

    Connect-SPOService -Url https://contoso-admin.sharepoint.com  
    $SiteURL = "https://crescent.sharepoint.com/sites/luyitest"  
    $ExportFile ="c:\ListRpt.csv"   
    Try {  
        #Get Credentials to connect  
        $Cred= Get-Credential  
       
        #Setup the context  
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
         
        #Get the web  
        $Web = $Ctx.web  
        $Ctx.Load($Web)  
           
        #get all subsites in sharepoint online powershell  
        $Ctx.Load($Web.Webs)  
        $Ctx.executeQuery()  
       
    $SubsiteCollection = @()  
    #Fetch each list item value to export to excel  
     $web.Webs |  foreach {  
        $ExportItem = New-Object PSObject  
        $ExportItem | Add-Member -MemberType NoteProperty -name "Title" -value $_.Title  
        $ExportItem | Add-Member -MemberType NoteProperty -Name "url" -value $_.url  
         
        #Add the object with the above properties to the Array  
        $SubsiteCollection += $ExportItem  
     }  
      
    #Export the result Array to CSV file  
    $SubsiteCollection | Export-CSV $ExportFile -NoTypeInformation  
       
    Write-host -f Green "List data Exported to CSV file successfully!"  
    }  
    Catch {  
        write-host -f Red "Error:" $_.Exception.Message  
    }  
    

    As a result, you will get a csv file like this:

    205013-image.png


    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.


0 additional answers

Sort by: Most helpful