PowerShell Script: Count Items in SharePoint Document Libraries
Key Takeaways:
- Audit Efficiently:
Quickly gather item counts across multiple SharePoint sites for inventory and compliance (e.g., GDPR) purposes. - Targeted Scope:
The script specifically targetsSPDocumentLibrarytypes and excludes hidden system libraries for cleaner reporting. - On-Premises Focus:
This specific script utilizes theSharePointSnapindesigned for on-premises SharePoint environments (2013, 2016, 2019, SE).

Why audit document library item counts with PowerShell?
Knowing exactly what information resides within your SharePoint solution is critical for governance, storage management, and compliance regulations like GDPR. Manual checking is impossible at scale. PowerShell allows administrators to programmatically iterate through sites and libraries to generate accurate reports on document distribution.
This is often the first step in broader [internal link: SharePoint security auditing] procedures, helping IT teams identify sprawling data repositories that may need archiving or tighter access controls.
The PowerShell Script for Counting SharePoint Documents
The following script is designed to list items in SharePoint document libraries that are not hidden. It connects to a specified site, finds all visible document libraries, counts the items within them, and outputs a formatted table containing the library URL/Title and the corresponding count.
Please ensure you replace http://your-sharepoint-site with your actual site URL before running.
PowerShell
<#
.SYNOPSIS
Lists item counts for non-hidden Document Libraries under a specific SharePoint Web.
.DESCRIPTION
This script utilizes the SharePoint Management Shell snap-in to query an SPWeb,
filter for visible SPDocumentLibrary types, count their items, and group the output.
.NOTES
Requirement: SharePoint Management Shell on-premises.
#>
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Get-SPWeb http://your-sharepoint-site |
Select -ExpandProperty Lists |
Where-Object {
$_.GetType().Name -eq "SPDocumentLibrary" -and -not $_.Hidden
} |
Select -ExpandProperty Items |
Group-Object {
$_.ParentList.ParentWeb.Url + "/" + $_.ParentList.Title
} |
Select-Object Name, Count |
Format-Table -AutoSize
How this script works
Here is a step-by-step breakdown of the pipeline used in this command:
Get-SPWeb http://your-sharepoint-site: This cmdlet retrieves the specific SharePoint web (site) object based on the provided URL.Select -ExpandProperty Lists: It takes the web object retrieved above and expands its “Lists” property. In SharePoint architecture, a document library is technically a specialized type of list.Where-Object { $_.GetType().Name -eq "SPDocumentLibrary" -and -not $_.Hidden }: This filters the stream. It accepts only objects that are specifically of the type “SPDocumentLibrary” AND are not marked as “Hidden” by the system.Select -ExpandProperty Items: It iterates through the filtered libraries and fetches all individual items (documents, folders) within each one.Group-Object { ... }: This is crucial for aggregation. It groups the individual items based on their parent list’s full URL and Title. This ensures items from the same library are counted together.Select-Object Name, Count: From the grouped results, it selects the name of the group (the library URL/Title constructed in the previous step) and the total count of items in that group.Format-Table -AutoSize: Finally, it presents the data in a readable table format, automatically adjusting column widths to fit the content.
Important Consideration: On-Premises vs. SharePoint Online
It is vital to note that the script above uses the Get-SPWeb cmdlet, which is part of the traditional SharePoint server-side object model.
This script will only work for on-premises deployments (such as SharePoint 2016, 2019, or Subscription Edition) and requires the SharePoint Management Shell to be loaded on the machine running the script.
If you are using Microsoft 365, you cannot use these cmdlets. Instead, you must use the SharePoint Online Management Shell or the community-driven PnP PowerShell. While the logic is similar, the connection methods and cmdlets differ significantly.
Execution Prerequisites
To successfully run this script, ensure the following:
- You are logged onto a SharePoint server in your farm, or a management server with the SharePoint snap-ins installed.
- You are running PowerShell as an Administrator.
- Your account has sufficient Read permissions on the SharePoint web site you are querying.
- If the SharePoint snap-in is not already loaded in your session, include the
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinueline provided in the script block above.
For those migrating to the cloud, learning how to perform similar audits using modern tools is essential. We recommend exploring resources on SharePoint Wiki for future-proofing your skills.

1 Response
[…] Remember, you can embed PowerShell commands into sharepoint. […]