SharePoint Online: Get Document Library Inventory (Folder-SubFolder-File Structure) using Powershell

Requirement: Get the file-folder structure of a SharePoint document library and export to CSV.

SharePoint Online: PowerShell to get the Document Library Inventory
Let's export all folders, sub-folders and files structure from a SharePoint Online document library to a CSV file:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL= ""
$ListName = "Documents"
$CSVPath = "C:\Temp\DocumentLibraryRpt.csv"
$BatchSize = 500
#Get Credentials to connect
$Cred = Get-Credential
Try {
    #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 Document Library
    $List =$Ctx.Web.Lists.GetByTitle($ListName)
    #Define CAML Query to Get List Items in batches
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml ="
    <View Scope='RecursiveAll'>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        <RowLimit Paged='TRUE'>$BatchSize</RowLimit>

    $DataCollection = @()
        #get List items
        $ListItems = $List.GetItems($Query) 

        #Iterate through each item in the document library
        ForEach($ListItem in $ListItems)
            #Collect data        
            $Data = New-Object PSObject -Property ([Ordered] @{
                Name  = $ListItem.FieldValues.FileLeafRef
                RelativeURL = $ListItem.FieldValues.FileRef
                Type =  $ListItem.FileSystemObjectType
                CreatedBy =  $ListItem.FieldValues.Author.Email
                CreatedOn = $ListItem.FieldValues.Created
                ModifiedBy =  $ListItem.FieldValues.Editor.Email
                ModifiedOn = $ListItem.FieldValues.Modified
                FileSize = $ListItem.FieldValues.File_x0020_Size
            $DataCollection += $Data
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    }While($Query.ListItemCollectionPosition -ne $null)

    #Export Documents data to CSV
    $DataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation
    Write-host -f Green "Document Library Inventory Exported to CSV!"
Catch {
    write-host -f Red "Error:" $_.Exception.Message

Get File-Folder Structure in a Document Library using PnP PowerShell
Similarly, If you want to export all file-folders structure of a document library to CSV file, use this PnP PowerShell script:
$SiteURL = ""
$ListName= "Non-Transaction"
$ReportOutput = "C:\Temp\ListInventory.csv"
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -UseWebLogin
#Array to store results
$Results = @()
#Get all Items from the document library
$List  = Get-PnPList -Identity $ListName
$ListItems = Get-PnPListItem -List $ListName -PageSize 500
Write-host "Total Number of Items in the List:"$List.ItemCount

$ItemCounter = 0 
#Iterate through each item
Foreach ($Item in $ListItems)
        $Results += New-Object PSObject -Property ([ordered]@{
            Name              = $Item["FileLeafRef"]
            Type              = $Item.FileSystemObjectType
            FileType          = $Item["File_x0020_Type"]
            RelativeURL       = $Item["FileRef"]
            CreatedBy         = $Item["Author"].Email
            CreatedOn         = $Item["Created"]
    Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting Metadata from Item '$($Item['FileLeafRef'])"          

#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
Write-host "Document Library Inventory Exported to CSV Successfully!"
and the output report of the script:
Get sharepoint online document library inventory using powershell
SharePoint Online: Get Document Library Inventory (Folder-SubFolder-File Structure) using Powershell SharePoint Online: Get Document Library Inventory (Folder-SubFolder-File Structure) using Powershell Reviewed by Salaudeen Rajack on March 03, 2019 Rating: 5


  1. What if you only want the folder structure, no file names

    1. Use the CAML Filter: <FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value>


Please Login and comment to get your questions answered!

Powered by Blogger.