I need a simple script that returns a count of Custom Lists and Document Libraries in a Site Collection. I don't need the items count or the files size totals, just the number of libraries.

Powershell or C# will work.


Try this script:

$username = "user@tenantname.onmicrosoft.com"
$password = "xxx"
$SiteCollectionUrl = "https://tenantname.sharepoint.com/Sites/sitename"
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 GetClientContext($SiteCollectionUrl, $username, $password) {
     $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
     $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl) 
     $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
     $context.Credentials = $credentials
     return $context

# function to loop through subsites
function getlistcount ($siteurl){
    $clientContext = GetClientContext $siteurl $username $password
    $rootWeb = $clientContext.Web
    $childWebs = $rootWeb.Webs
    #do something on top level site
    write-host $rootWeb.url -ForegroundColor Yellow
    foreach ($childWeb in $childWebs)

          write-host $childWeb.url -ForegroundColor Yellow

     write-host ($count)

getlistcount $SiteCollectionUrl
  • this only works for subsites and doesn't give the top level count – Michael Colbs Dec 20 '17 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.