Introduction

Hi guys, let’s try to understand the easiest way to analyze a List/Library both in Classic and Modern Online SharePoint Environment only.
The following is a cool CSOM PS Script to analyse a List with, including its Item Count, Folder Count, etc. The same code can be used to analyse a Lib, including its Item Count, Files Count, Folder Count, etc.
  1. #Load SharePoint CSOM Assemblies
  2. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
  3. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  4. #Config Parameters
  5. $SiteURL= "https://abcdcorp.sharepoint.com/ltms"
  6. $ListName = "TestSupplyListPowerApp"
  7. #Setup Credentials to connect
  8. $Cred = Get-Credential
  9. Try {
  10. #Setup the context
  11. $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
  12. $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  13. #Get the List
  14. $List= $Ctx.web.lists.GetByTitle($ListName)
  15. $Ctx.Load($List)
  16. $Ctx.ExecuteQuery()
  17. #Define Query to Filter and Get All Files from the list
  18. $Query = "@
  19. <View Scope='RecursiveAll'>
  20. <Query>
  21. <Where>
  22. <Eq>
  23. <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
  24. </Eq>
  25. </Where>
  26. </Query>
  27. </View>"
  28. $FilesQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
  29. $FilesQuery.ViewXml =$Query
  30. $Files = $List.GetItems($FilesQuery)
  31. $Ctx.Load($Files)
  32. $Ctx.ExecuteQuery()
  33. #Define Query to Filter and Get All Folders from the list
  34. $Query = "@
  35. <View Scope='RecursiveAll'>
  36. <Query>
  37. <Where>
  38. <Eq>
  39. <FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value>
  40. </Eq>
  41. </Where>
  42. </Query>
  43. </View>"
  44. $FoldersQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
  45. $FoldersQuery.ViewXml =$Query
  46. $Folders = $List.GetItems($FoldersQuery)
  47. $Ctx.Load($Folders)
  48. $Ctx.ExecuteQuery()
  49. #Get List Item Count
  50. Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
  51. Write-host -f Green "Total Number of Files in the List:"$Files.Count
  52. Write-host -f Green "Total Number of Folders in the List:"$Folders.Count
  53. }
  54. Catch {
  55. write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
  56. }
Just open Windows PowerShell ISE and run the below Script after configuring your List/Library name along with its respective Site Collection.
Provide your User Account details when prompted in a Pop-Up. You will get the below result as shown in the below snapshot:
Happy SharePointing!