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

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

9 thoughts on “SharePoint Online: Get Document Library Inventory (Folder-SubFolder-File Structure) using PowerShell

  • July 2, 2020 at 11:07 AM

    Can I just get the File name instead of File name with extension? (I need to remove extension!)

    • July 2, 2020 at 11:16 AM

      There are no properties to get just the file name. However, You can use: $Item.FieldValues.FileLeafRef.Replace(“.$($Item.FieldValues.File_x0020_Type)”,””)

  • June 17, 2020 at 6:09 PM

    How to get the file and folder structure from an entire site instead of changing document library name one by one?

  • April 30, 2020 at 8:35 PM

    ok i didn’t realize you have to change the list name for each library and run the script again. Awesome! I got what I needed, thanks again for the excellently written script!

  • April 30, 2020 at 7:56 PM

    ok i got around the error messages by replacing the first two lines with:

    but for some reason, its only listing one file in the CSV 🙁 so close but I’m still missing something

  • April 30, 2020 at 7:39 PM

    thanks for this awesome script! saves me a lot of time (I hope) Do you happen to know what i’m doing wrong to get this error?

    PS C:WINDOWSsystem32> #Load SharePoint CSOM Assemblies
    PS C:WINDOWSsystem32> Add-Type -Path “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll”
    Add-Type : Cannot bind parameter ‘Path’ to the target. Exception setting “Path”: “Cannot find path ‘C:Program
    FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll’ because it does
    not exist.”
    At line:1 char:16
    + … -Type -Path “C:Program FilesCommon FilesMicrosoft SharedWeb Serve …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : WriteError: (:) [Add-Type], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.AddTypeCommand

  • March 20, 2020 at 6:58 PM

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


Leave a Reply