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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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
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