-1

I have a document library which contains many folders. I want to get count of items within each folder in this library.

I would like to understand if this can be achieved using CSOM Powershell.

Is there any OOTB way?

user2598808
  • 633
  • 5
  • 22
  • 40

2 Answers2

0

Just as a forewarning, I am not sure CSOM Powershell has the same cmdlets as traditional powershell. Let me know if they do and I'll get rid of this post. If not, this should work:

Assuming a structure where you have [library] containing [folders] containing all the files you want to count.

foreach($folder in (Get-ChildItem $[library])){  
    $filecount = 0
    Get-ChildItem $folder -recurse |
    %{$filecount += 1}
    #do whatever you want with $filecount here.
}

You could use a dictionary or another data structure to keep count of what folders have how many items, outside the foreach loop.

Maxime Franchot
  • 1,015
  • 1
  • 10
  • 24
0

The number of files in folder could be retrieved via ItemChildCount field, here is an example that demonstrates how to get the files number per folder:

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$ctx.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
$list = $ctx.Web.Lists.GetByTitle("Documents")

$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View Scope='RecursiveAll'>
<ViewFields>
    <FieldRef Name='FileRef' />
    <FieldRef Name='ItemChildCount' />
</ViewFields>
<Query>
    <Where>
       <Eq>
         <FieldRef Name='FSObjType' />
         <Value Type='Integer'>1</Value>
       </Eq>
   </Where>
</Query>
</View>"

$items = $list.GetItems($query)
$ctx.Load($items) 
$ctx.ExecuteQuery()


$items.GetEnumerator() | % {

   Write-Host $_["FileRef"] , ":" , $_["ItemChildCount"]
}

$ctx.Dispose() 
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193