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 :
  1. Credentials - Use Tenant Admin credentials when prompted for credentials
  2. 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
  1. ItemCount.Csv - List of all libraries with file count
  2. ItemsDetail.Csv - List of all the files in the library with their name
    1. # Input Parameters
    2. $credentials = Get - Credential
    3. $URL = Read - Host - Prompt 'Please enter admin site collection URL it would be in the format https://<<tenantname>>-admin.sharepoint.com'
    4. # Connect to SharePoint Online
    5. Connect - PnPOnline - Url $URL - Credentials $credentials
    6. # Get the site collections
    7. $siteColl = Get - PnPTenantSite
    8. # Loop through the site collections
    9. foreach($site in $siteColl) {
    10. write - host - ForegroundColor Green "Getting Data from site: "
    11. $site.Url
    12. Connect - PnPOnline - Url $site.Url - Credentials $credentials
    13. #Get all document libraries - Exclude Hidden Libraries
    14. $DocumentLibraries = Get - PnPList | Where - Object {
    15. $_.BaseTemplate - eq 101 - and $_.Hidden - eq $false
    16. }
    17. #Or $_.BaseType - eq "DocumentLibrary"
    18. #Get Document Libraries Name, Default URL and Number of Items
    19. $DocumentLibraries | Select Title, DefaultViewURL, ItemCount | Export - Csv - Path.\ItemCount.Csv - Append
    20. ForEach($DocumentLibrary in $DocumentLibraries) {
    21. #Get All Files from the document library - In batches of 500
    22. $ListItems = Get - PnPListItem - List $DocumentLibrary.Title - PageSize 500 | Where {
    23. $_["FileLeafRef"] - like "*.*"
    24. }
    25. $DocumentsData = @()
    26. ForEach($Item in $ListItems) {
    27. #Collect Documents Data
    28. $DocumentsData += New - Object PSObject - Property @ {
    29. FileName = $Item.FieldValues['FileLeafRef']
    30. FileURL = $Item.FieldValues['FileRef']
    31. }
    32. }
    33. $DocumentsData | Export - Csv - Path.\ItemsDetail.Csv - Append
    34. }
    35. $Subsites = Get - PnPSubWebs - Recurse
    36. $Subsiteurl = $site.Url - split "/"
    37. ForEach($Subsite in $Subsites) {
    38. $FinalSubsiteurl = "https://" + $Subsiteurl[2] + $Subsite.ServerRelativeUrl
    39. write - host - ForegroundColor Green "Getting Data from site: "
    40. $FinalSubsiteurl
    41. Connect - PnPOnline - Url $FinalSubsiteurl - Credentials $credentials
    42. #Get all document libraries - Exclude Hidden Libraries
    43. $DocumentLibraries = Get - PnPList | Where - Object {
    44. $_.BaseTemplate - eq 101 - and $_.Hidden - eq $false
    45. }
    46. #Or $_.BaseType - eq "DocumentLibrary"
    47. #Get Document Libraries Name, Default URL and Number of Items
    48. $DocumentLibraries | Select Title, DefaultViewURL, ItemCount | Export - Csv - Path.\ItemCount.Csv - Append
    49. ForEach($DocumentLibrary in $DocumentLibraries) {
    50. #Get All Files from the document library - In batches of 500
    51. $ListItems = Get - PnPListItem - List $DocumentLibrary.Title - PageSize 500 | Where {
    52. $_["FileLeafRef"] - like "*.*"
    53. }
    54. $DocumentsData = @()
    55. ForEach($Item in $ListItems) {
    56. #Collect Documents Data
    57. $DocumentsData += New - Object PSObject - Property @ {
    58. FileName = $Item.FieldValues['FileLeafRef']
    59. FileURL = $Item.FieldValues['FileRef']
    60. }
    61. }
    62. $DocumentsData | Export - Csv - Path.\ItemsDetail.Csv - Append
    63. }
    64. }
    65. }