How to Get Count of Items in SharePoint List using PnP PowerShell?

Recently, I got a requirement to display the SharePoint Online list item count. I used PnP PowerShell for this. In this tutorial, I will show you how to get the count of items in a SharePoint list using PnP PowerShell.

Get Count of Items in SharePoint List using PnP PowerShell

Let me show you different methods to get the count of items in a SharePoint list using PnP PowerShell.

Here, I have a SharePoint Online list to which I have added a few items. The screenshot below shows what the SharePoint list looks like.

get count of items in sharepoint list

We will see here how to get the SharePoint list item counts using PnP PowerShell.

1. Get List Item Count Using Get-PnPList

In PnP PowerShell, you can use the the ItemCount property of the list object to get the item count in SharePoint Online.

Here is a complete PnP PowerShell script, you can see.

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Get the list
$list = Get-PnPList -Identity "LargeInventoryList"

# Get the item count
$itemCount = $list.ItemCount

Write-Output "Total items in the list: $itemCount"

Once you execute the above PowerShell script using VS code, you can see the SharePoint Online list item count like in the screenshot below. I executed the above script in my local system.

get count of items in sharepoint list using powershell

Read How to Check if a Folder Exists in SharePoint Online using PnP PowerShell

2. Get List Item Count Using Get-PnPListItem

The PnP PowerShell Get-PnPListItem cmdlet retrieves items from a SharePoint list. To get the count of items, you can use the -PageSize parameter to handle large lists efficiently.

Here is a complete PnP PowerShell script.

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Get the list
$list = Get-PnPList -Identity "LargeInventoryList"

# Get all items in the list
$items = Get-PnPListItem -List $list -PageSize 5000 -Fields "ID"

# Get the count of items
$itemCount = $items.Count

Write-Output "Total items in the list: $itemCount"

This method is particularly useful if the list is large. I executed the above PowerShell script using VS code, and you can see the output in the screenshot below. It gives me the exact list of item counts.

how to get count of items in sharepoint list using pnp powershell

Read Upload Files to a SharePoint Document Library using PnP PowerShell

3. Get Item Count from a Specific Folder

If you need to get the count of items in a specific folder within a SharePoint list or library, you can use the Get-PnPFolderItem PnP PowerShell cmdlet.

Here is a complete PowerShell script.

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Define the library and folder
$libraryName = "MyLibrary"
$folderPath = "/sites/PowerPlatformFAQs/MyLibrary/MyFolder"

# Get the items in the folder
$folderItems = Get-PnPListItem -List $libraryName -FolderServerRelativeUrl $folderPath -PageSize 5000

# Get the count of items in the folder
$itemCount = $folderItems.Count

Write-Output "Total items in the folder: $itemCount"

The Get-PnPListItem cmdlet is used with the -FolderServerRelativeUrl parameter to specify the folder path. This retrieves all items within the specified folder.

I executed the above PnP PowerShell script, and you can see in the screenshot below it is giving the exact item count from a SharePoint Online list or library folder.

get number of items in sharepoint list folder using pnp powershell

Conclusion

In this tutorial, I have explained how to get the count of items in a SharePoint list using PnP PowerShell. I have also explained how to count items in a large SharePoint Online list using PnP PowerShell. We also saw how to count items from a folder in a SharePoint list or document library using PnP PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.