How to count specific folder items in SharePoint site using PowerShell

Aase Nomad 246 Reputation points
2022-04-20T14:04:51.833+00:00

I have onprem SharePoint site and I wanted to count all the items total number for a specific folder under "Shared Documents" List so I would be really appreciated if I can get any suggestion or help on how to modify this powershell CSOM script.

I found this example script online and I'm not sure how to modify it to get the total item count for one folder instead of every folder.

So basically I have "Jon" Folder under "Shared Documents" List and I want to count from only that folder.

I need to use CSOM method because there is issue with connecting with pnpmodule in my tenant that's why.

#Load SharePoint CSOM Assemblies
    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 Get-SPOListItemCount{
        [CmdletBinding()]
        [OutputType([int])]
        Param
        (
            [Parameter(Mandatory=$true, HelpMessage="The URL of the site that contains the list or library", Position=0)] [string]$WebUrl,
            [Parameter(Mandatory=$true, HelpMessage="The Title of the list or library", Position=1)] [string]$ListName
        )

        Begin{

            #Setup context to connect
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
            $Ctx.Credentials =  New-Object System.Net.NetworkCredential("username", "password")

        }
        Process{
            try{
                #Get the Web
                $Web = $Ctx.Web
                #Get the List
                $List = $web.Lists.GetByTitle($ListName)
                $Ctx.Load($List)
                $Ctx.ExecuteQuery()
                #sharepoint online count items in list
                Return $List.ItemCount
            }
            catch{
                Write-Host -ForegroundColor Red $_.Exception.Message
                return 0
            }

        }
        End{
            $Ctx.Dispose()
        }
    }

    #Call the function to get item count
    Get-SPOListItemCount -WebUrl "https://share.sp.com/sites/Campaign" -ListName "Shared Documents"
Microsoft 365 and Office SharePoint Server For business
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-04-22T09:31:44.797+00:00

    Hi @Aase Nomad ,

    According to my research and testing, please get the subfolders in the list first and then get the number of items in the specified subfolder.

    Please modify the code in the script , as follows:

               #Get the Web  
                $Web = $Ctx.Web  
                #Get the List  
                $List = $web.Lists.GetByTitle($ListName)  
                $Ctx.Load($List)  
                $Ctx.ExecuteQuery()  
                #sharepoint online count items in list  
      
                $subfolders=$List.RootFolder.Folders  
                $Ctx.Load($List.RootFolder.Folders)  
                $Ctx.ExecuteQuery()  
                foreach ($folder in $subfolders)  
                {  
                    if ($folder.name -eq "<<specific folder name>>"){  
                        return $folder.ItemCount  
                    }  
                }  
    

    My test results:

    items in the subfolder: 195410-image.png Items count results: 195553-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.

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.