Get All Items in a Document Library (PnP PowerShell)

Get All Items in a Document Library (PnP PowerShell)

Issue

SharePoint Online has a limitation of the number of items that can be retreived at one time. If you have a large list or library, you cannot use Get-PnpListItem to simply get all items.

Instead, we need to page our way through the items to get all the items. For a library of 500,000 items, it took a few mnutes to pull down all of the metadata.

If there are additional fields you want to capture, add them to the -Fields parameter.

The Code

# Variables
$Domain = "<tenant-name>"
$SiteName = "<site-name>"
$ListName = "<list-name>"
$SiteURL = "https://$Domain.sharepoint.com/sites/$SiteName"

#Connect to Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List
$List =  Get-PnPList -Identity $ListName

#Get all Folders from List - with progress bar
$global:counter = 0;
$Items = Get-PnPListItem -List $List -PageSize 5000 `
-Fields FileLeafRef `
-ScriptBlock { 
    Param($items) $global:counter += $items.Count
    Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Folders from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";
}

Credits

The original code I found on SharePoint Diary a long time ago and I've had it kicking around in various scripts for a while.

Documenting this to find it more easily in the future. Claiming no credit for the original.

Last updated