Overview
In this blog we will use PnP PowerShell and fetch item count and item details for all the libraries in the SharePoint online tenant.
Pre-Requisite
PnP PowerShell should be installed. If not please install it using the below command for Windows 10 users or use this
link for installation
Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser
Script
Save the below PS script on your local Path. Run it from your Windows PowerShell ISE.
Inputs which will be required :
- Credentials - Use Tenant Admin credentials when prompted for credentials
- Tenant Admin URL - Please enter admin site collection URL it would be in the format https://<<tenantname>>-admin.sharepoint.com
Output
Two files will be created in the same directory where the PowerShell script is present
- ItemCount.Csv - List of all libraries with file count
- ItemsDetail.Csv - List of all the files in the library with their name
- # Input Parameters
- $credentials = Get - Credential
- $URL = Read - Host - Prompt 'Please enter admin site collection URL it would be in the format https://<<tenantname>>-admin.sharepoint.com'
- # Connect to SharePoint Online
- Connect - PnPOnline - Url $URL - Credentials $credentials
- # Get the site collections
- $siteColl = Get - PnPTenantSite
- # Loop through the site collections
- foreach($site in $siteColl) {
- write - host - ForegroundColor Green "Getting Data from site: "
- $site.Url
- Connect - PnPOnline - Url $site.Url - Credentials $credentials
- #Get all document libraries - Exclude Hidden Libraries
- $DocumentLibraries = Get - PnPList | Where - Object {
- $_.BaseTemplate - eq 101 - and $_.Hidden - eq $false
- }
- #Or $_.BaseType - eq "DocumentLibrary"
- #Get Document Libraries Name, Default URL and Number of Items
- $DocumentLibraries | Select Title, DefaultViewURL, ItemCount | Export - Csv - Path.\ItemCount.Csv - Append
- ForEach($DocumentLibrary in $DocumentLibraries) {
- #Get All Files from the document library - In batches of 500
- $ListItems = Get - PnPListItem - List $DocumentLibrary.Title - PageSize 500 | Where {
- $_["FileLeafRef"] - like "*.*"
- }
- $DocumentsData = @()
- ForEach($Item in $ListItems) {
- #Collect Documents Data
- $DocumentsData += New - Object PSObject - Property @ {
- FileName = $Item.FieldValues['FileLeafRef']
- FileURL = $Item.FieldValues['FileRef']
- }
- }
- $DocumentsData | Export - Csv - Path.\ItemsDetail.Csv - Append
- }
- $Subsites = Get - PnPSubWebs - Recurse
- $Subsiteurl = $site.Url - split "/"
- ForEach($Subsite in $Subsites) {
- $FinalSubsiteurl = "https://" + $Subsiteurl[2] + $Subsite.ServerRelativeUrl
- write - host - ForegroundColor Green "Getting Data from site: "
- $FinalSubsiteurl
- Connect - PnPOnline - Url $FinalSubsiteurl - Credentials $credentials
- #Get all document libraries - Exclude Hidden Libraries
- $DocumentLibraries = Get - PnPList | Where - Object {
- $_.BaseTemplate - eq 101 - and $_.Hidden - eq $false
- }
- #Or $_.BaseType - eq "DocumentLibrary"
- #Get Document Libraries Name, Default URL and Number of Items
- $DocumentLibraries | Select Title, DefaultViewURL, ItemCount | Export - Csv - Path.\ItemCount.Csv - Append
- ForEach($DocumentLibrary in $DocumentLibraries) {
- #Get All Files from the document library - In batches of 500
- $ListItems = Get - PnPListItem - List $DocumentLibrary.Title - PageSize 500 | Where {
- $_["FileLeafRef"] - like "*.*"
- }
- $DocumentsData = @()
- ForEach($Item in $ListItems) {
- #Collect Documents Data
- $DocumentsData += New - Object PSObject - Property @ {
- FileName = $Item.FieldValues['FileLeafRef']
- FileURL = $Item.FieldValues['FileRef']
- }
- }
- $DocumentsData | Export - Csv - Path.\ItemsDetail.Csv - Append
- }
- }
- }