Share via

How to count specific folder items in SharePoint site using PowerShell

Aase Nomad 246 Reputation points
Apr 20, 2022, 2:04 PM

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"
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
3,037 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,628 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    Apr 22, 2022, 9:31 AM

    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.