SharePoint Online: Get List Item Count using PowerShell
Requirement: Get list item count in SharePoint Online using PowerShell.
How to Get List Items Count in SharePoint Online?
If you need to get a count of the items in a given SharePoint Online list, there are a few ways to do it. In this post, we’ll show you how to use PowerShell-CSOM to get the item count for any SharePoint Online list. We’ll also show you how to use the PnP PowerShell to get the list items count. This can be useful if you have to generate a report with this information. Let’s get started!
The “Site Contents” page in SharePoint Online gives you the number of items in each list and library, along with the name, type, and last modified date information. (URL: https://<tenant>.sharepoint.com/_layouts/Viewlsts.aspx)
SharePoint Online: PowerShell to Get List Item Count
Are you looking to get a list item count using PowerShell in SharePoint Online? Use this CSOM PowerShell script to find the number of items in a SharePoint Online list:
#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"
#Setup 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 List
$List= $Ctx.web.lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get List Item Count
Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
}
Catch {
write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
}
Let’s wrap the above script into a re-usable function and get the list item count for a List or document library.
#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"
Function Get-SPOListItemCount{
<#
.SYNOPSYS
Returns the Item Count for the specified list or library
.DESCRIPTION
Returns the Item Count for the specified list or library
.PARAMETER WebUrl
The URL of the site that contains the list or library
.PARAMETER ListName
The title of the list or library to get count
.EXAMPLE
Get-SPOListItemCount -WebUrl "https://tenant.sharepoint.com/teams/marketing" -ListName "Shared Documents"
#>
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true, HelpMessage="The URL of the site that contains the list or library", Position=0)] [string]$WebUrl,
[Parameter(Mandatory=$true, HelpMessage="The Title of the list or library", Position=1)] [string]$ListName
)
Begin{
#Get Credentials to connect
$Credential = Get-Credential
#Setup context to connect
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName, $Credential.Password)
}
Process{
try{
#Get the Web
$Web = $Ctx.Web
#Get the List
$List = $web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#sharepoint online count items in list
Return $List.ItemCount
}
catch{
Write-Host -ForegroundColor Red $_.Exception.Message
return 0
}
}
End{
$Ctx.Dispose()
}
}
#Call the function to get item count
Get-SPOListItemCount -WebUrl "https://crescent.sharepoint.com/sites/marketing" -ListName "Documents"
This PowerShell script gets the list item count in SharePoint Online
Get List Item Count in SharePoint Online using PnP PowerShell
Let’s get a number of items from all lists in a SharePoint Online site. This can be useful if you need to get an accurate count of the number of files in a library for reporting or other purposes.
#Variable
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
#Connect to PnP Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#Get List Item count from all Lists from the Web
Get-PnPList | Select Title, ItemCount
You can also use this PowerShell script to count items in the document library in SharePoint Online. Similarly, you can filter list items based on a specific condition and get its count:
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Tasks"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get list items - Filtered by given condition
$ListItems = Get-PnPListItem -List $ListName -Query "<View><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where></Query></View>"
#Get List Items Count
$ListItems.Count
How to Export List Item Count for All lists in a Site Collection?
Here is how to export item count from all lists and libraries from a site collection using PnP PowerShell to CSV format.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\temp\ListItemCount.csv"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Webs from the site collection
$SubWebs = Get-PnPSubWeb -Recurse -IncludeRootWeb
$ListInventory= @()
Foreach ($Web in $SubWebs)
{
Write-host -f Yellow "Getting List Item count from site:" $Web.URL
#Connect to Subweb
Connect-PnPOnline -Url $Web.URL -Interactive
#Get all lists and libraries of the Web
$ExcludedLists = @("Reusable Content","Content and Structure Reports","Form Templates","Images","Pages","Workflow History","Workflow Tasks", "Preservation Hold Library")
$Lists= Get-PnPList | Where {$_.Hidden -eq $False -and $ExcludedLists -notcontains $_.Title}
foreach ($List in $Lists)
{
$Data = new-object PSObject
$Data | Add-member NoteProperty -Name "Site Name" -Value $Web.Title
$Data | Add-member NoteProperty -Name "Site URL" -Value $Web.Url
$Data | Add-member NoteProperty -Name "List Title" -Value $List.Title
$Data | Add-member NoteProperty -Name "List URL" -Value $List.RootFolder.ServerRelativeUrl
$Data | Add-member NoteProperty -Name "List Item Count" -Value $List.ItemCount
$ListInventory += $Data
}
}
$ListInventory | Export-CSV $CSVFile -NoTypeInformation
Write-host -f Green "List Inventory Exported to Excel Successfully!"
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Result:
What if you want to count files in a document library?
SharePoint Online: Count Items in Document Library using PowerShell
Because the ItemCount property of a SharePoint Online list gives you the count of items, including files and folders together, How do we count files and folders in a library? Well, here is the PowerShell to get the count of files and folders:
#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"
#Setup 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 List
$List= $Ctx.web.lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Define Query to Filter and Get All Files from the list
$Query = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
</Eq>
</Where>
</Query>
</View>"
$FilesQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$FilesQuery.ViewXml =$Query
$Files = $List.GetItems($FilesQuery)
$Ctx.Load($Files)
$Ctx.ExecuteQuery()
#Define Query to Filter and Get All Folders from the list
$Query = "@
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value>
</Eq>
</Where>
</Query>
</View>"
$FoldersQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$FoldersQuery.ViewXml =$Query
$Folders = $List.GetItems($FoldersQuery)
$Ctx.Load($Folders)
$Ctx.ExecuteQuery()
#Get List Item Count
Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
Write-host -f Green "Total Number of Files in the List:"$Files.Count
Write-host -f Green "Total Number of Folders in the List:"$Folders.Count
}
Catch {
write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
}
You can also loop through list items and check $ListItem.FileSystemObjectType -eq “File” or “Folder” to get Files and Folders count. Here is my other post to get large lists in SharePoint Online Site SharePoint Online: How to Find All Large Lists using PowerShell?
Hi. Fantastic scripts!
I am trying to get all list in a sitecollection and count all files and folders. It works fine, but now I want to only count files that has not been modified for a year, but It will not work. Here is the count part of the script:
$Data | Add-member NoteProperty -Name “Elements” -Value $List.Items | Where-Object { $_.LastModified -gt (Get-Date).AddDays(-365)}.Count
Thank you for a great blog 🙂
This should work: Where {$_.FieldValues.Modified -lt (Get-Date).AddDays(-365)} . Reference: SharePoint Online: Delete All Files Older than 30 Days in a Document Library using PowerShell
Hi,
Great post am struggling to loop through all sites in my site collection. Can you advise or help how I can loop through all sites to get item count?
Refer here: PowerShell to Iterate through All Subsites in a SharePoint Online Site Collection
Thanks for the solution. Is there a way to get the item count for a specific view in a list/document library?
Sure! Here is the reference: Get All Items from a List View in SharePoint Online using PowerShell
Do you have ide how I can get pages count of PDF using PowerShell, I am using SharePoint Online
THanks for this.
Testing out How to Export List Item Count for All lists in a Site Collection?
I was getting the error for the Get Credentials variable.
Error: Parameter set cannot be resolved using the specified named parameters.
I noticed you have the connect-pnponline command to -Interactive but then also to use the variable $Cred.
I think this is where it’s incompatible.
I simple hashed out the $Cred = Get-Credential and then changed the connect to Connect-PnPOnline -Url $SiteURL -Interactive
All good after this.
I did the same and was able to connect but the script is now giving this warning “WARNING: Parameter ‘Web’ is obsolete. The -Web parameter will be removed in a future release. Use Connect-PnPOnline -Url [subweburl] instead to connect to a subweb.”
It gets stuck and does not iterate through all the sites?
Thanks
Yes! In the latest PnP.PowerShell module, -Web Parameter is obsolete. Post is updated with that change now!
That works and many many thanks for the prompt response, I appreciate it! 🙂
Mark