How should I get file count for only one SharePoint folder in PowerShell

Aase Nomad 246 Reputation points
2022-02-19T00:57:40.28+00:00

In my SharePoint site, I have two folders called "General" and "Testing2" under Document. I'm trying to get the count only for "General" but not sure why it's still also counting for Testing2 folder so I would be really appreciate if I can get any help or suggestion.

I tried like this, $ListName = "/Shared Documents/General" but still not working

   #Parameters  
   $SiteURL = "https://comapny.sharepoint.com/sites/msteams_####/"  
     
   //I tried like this but still not working  
   $ListName = "/Shared Documents/General"  
   $CSVFile = "C:\Users\kek\Desktop\Resource\LitHold\FolderStats.csv"  
      
   #Connect to SharePoint Online  
   Connect-PnPOnline $SiteURL -useWebLogin  
       
   #Get the list  
   $List = Get-PnPList -Identity $ListName  
      
   #Get Folders from the Library - with progress bar  
   $global:counter = 0  
   $FolderItems = Get-PnPListItem -List $ListName -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `  
               ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Items from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";}  | Where {$_.FileSystemObjectType -eq "Folder"}  
   Write-Progress -Activity "Completed Retrieving Folders from List $ListName" -Completed  
      
   $FolderStats = @()  
   #Get Files and Subfolders count on each folder in the library  
   ForEach($FolderItem in $FolderItems)  
   {  
       #Get Files and Folders of the Folder  
       Get-PnPProperty -ClientObject $FolderItem.Folder -Property Files, Folders | Out-Null  
          
       #Collect data  
       $Data = [PSCustomObject][ordered]@{  
           FolderName     = $FolderItem.FieldValues.FileLeafRef  
           URL            = $FolderItem.FieldValues.FileRef  
           FilesCount     = $FolderItem.Folder.Files.Count  
           SubFolderCount = $FolderItem.Folder.Folders.Count  
     
       }  
       $Data  
       $FolderStats+= $Data  
   }  
   #Export the data to CSV  
   $FolderStats | Export-Csv -Path $CSVFile -NoTypeInformation  

 

I don't want to to print Testing 2

175986-vdnj8.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,599 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,949 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,514 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rob Windsor 1,961 Reputation points
    2022-02-19T09:14:53.567+00:00

    Use Get-PnPFolder to get the folder and then use the ItemCount property of the folder object to get the number of items.

    https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpfolder

    Screen capture of PowerShell commands


  2. Alcala, Jesus 6 Reputation points
    2022-07-04T13:31:04.327+00:00

    You can use this to get the item count for a specific libary

    Get-PnPList -Identity "List Name" | Select-Object ItemCount  
    

    Using the variables in your code:

    $ListName = "General"  
      
    $FolderItems = Get-PnPList -Identity $ListName | Select-Object ItemCount  
    

    For the Items List you can use

    $xmlQuery =  "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>"  
    $ListItems = Get-PnPListItem -List $ListName -PageSize 5000 -Query $xmlQuery  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.