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 it to CSV.

SharePoint Online: PowerShell to get the Document Library Inventory

Are you looking for a way to get an inventory of all the documents stored in your SharePoint Online Document Library? Well, Here is the handy PowerShell that can help! In this blog post, we will show you how to use PowerShell to generate a report that lists all the documents and their properties, so you can keep track of what documents are stored in the library.

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= "https://crescent.sharepoint.com/sites/marketing"
$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'>
        <Query>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit Paged='TRUE'>$BatchSize</RowLimit>
    </View>"

    $DataCollection = @()
    Do
    {
        #get List items
        $ListItems = $List.GetItems($Query) 
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery() 

        #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:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/RI/"
$ListName= "Non-Transaction"
$ReportOutput = "C:\Temp\ListInventory.csv"
 
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
 
#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"]
        })
    $ItemCounter++
    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!

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

  • Hi,
    what would be the exact term for the “Get-PnPListItem”? I have a big library with 4M files where I need to retreieve just the folder structure (of a specific subfolder). Even when I use “FolderServerRelativeUrl” and a PageSize of 5000, which definitly fits the folder count it throws me an error saying that it exceeds the max threshold…

    Reply
  • Can we have any PowerShell script that we used for get user permission report for SharePoint site

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

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

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

    Reply
  • 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!

    Reply
  • ok i got around the error messages by replacing the first two lines with:
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”)
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”)

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

    Reply
  • 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 Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll”
    Add-Type : Cannot bind parameter ‘Path’ to the target. Exception setting “Path”: “Cannot find path ‘C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.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

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

    Reply

Leave a Reply