SharePoint Online, PowerShell, Get all sites in a site collection and document libraries/lists

Ever wanted a PowerShell script to loop through all of your site collection's webs and then grab all of the document libraries and lists? Try this script out. Big thanks to this article, I used this code but altered it to use one site collection, write to the file at the very end (was running too fast and getting errors due to the file being locked), have an exclusion list and other bits n pieces. Code has been tested against a real environment.

Make sure you install the SharePoint Online Management Shell and the SharePoint Online Client Components SDK.

Here is a screenshot of the output. Has item counts for each library. Outputs file in same folder as your .ps1 script.

function connectToO365{ 
    $adminUrl = ""
    $userName = "ADMIN"
    $password = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force
    Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
        Connect-SPOService -Url $adminUrl -Credential $credentials
        write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
        write-host "Error: Could not connect to Office 365" -foregroundcolor red
        Break connectToO365
    #create HTML file
    $filePath = create-outputfile

    #add start HTML information
    $global:textToWrite += "
    <h1>Site structure</h1>
    <table border='0' style='font-family: Calibri, sans-serif; padding: 5px;'>
            <th style='background-color:#454545; color:white'>URL</th>
            <th style='background-color:#454545; color:white'>Type</th>
            <th style='background-color:#454545; color:white'>Template</th>
            <th style='background-color:#454545; color:white'>Item Count</th>
    $global:textToWrite += "<tr style='background-color:#95B9C7'><td>$($url)</td><td>Site Collection</td><td></td><td></td></tr>"
    write-host "Info: Found $($url)" -foregroundcolor green
    $AllWebs = Get-SPOWebs($url)

    $global:textToWrite += "

    #finally, write everything to file
    add-content -value $textToWrite -path $filePath
function create-outputfile(){ 
    $date = get-date -format dMMyyyyhhmm
    $filePath = "$($PSScriptRoot)\Output$($date).html"
    if (!(Test-Path -path $filePath)){
        #create file
        New-Item $filePath -type file | out-null 
        write-host “File created: $($filePath)” -foregroundcolor green
        write-host "Output file already exists, wait 1 minute" -foregroundcolor yellow
        Break create-outputfile
    return $filePath
function Get-SPOWebs($url){ 
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    $context.Credentials = $SPOcredentials
    $web = $context.Web
        foreach($list in $web.lists){
            if ($excludeLists -notcontains $list.Title)
                write-host "List included " $list.Title -foregroundcolor green
                $global:textToWrite += "<tr><td><span style='margin-left:$($pixelslist)px'>$($list.title)</td><td></td><td></td><td>$($list.itemcount)</td></tr>"
                write-host "List excluded " $list.Title -foregroundcolor blue
        $pixelsweb = $pixelsweb + 15
        $pixelslist = $pixelslist + 15
        foreach($web in $web.Webs) {
            $global:textToWrite += "<tr style='background-color:#999999'><td><span style='margin-left:$($pixelsweb)px'>$($web.url)</td><td>Web</td><td>$($web.webtemplate)</td><td></td></tr>"
            write-host "Info: Found $($web.url)" -foregroundcolor green
        write-host "Could not find web" -foregroundcolor red

write-host "Kicking Off" -foregroundcolor green

$excludeLists = @("Master Page Gallery",
                "User Information List",
                "Theme Gallery",
                "Style Library",
                "Solution Gallery",
                "Reusable Content",
                "Reporting Templates",
                "Reporting Metadata",
                "Long Running Operation Status",
                "List Template Gallery",
                "Content and Structure Reports",
                "Cache Profiles",
                "Workflow History",
                "Web Part Gallery",
                "Form Templates",
                "Workflow Tasks",
                "Suggested Content Browser Locations",
                "Site Template Gallery",
                "Variation Labels",
                "Content type publishing error log",
                "Converted Forms"

# create variable to write to, later add to file
$textToWrite = ''

#go do all the things

Write-Host "Completed!" -foregroundcolor Yellow


Leave a Reply

Your email address will not be published. Required fields are marked *