Share via


Powershell script to count number of list and library items in site collection

Question

Thursday, July 10, 2014 10:02 PM

We are identifying large lists in our 2010 SP environment and I'm attempting to write code to output the total number of list items in a site collection.   I'm using the code below but it only displays column data for title and url.  What do I need to add so it can count the number of items in each sharepoint list and library? 

Get-SPSite -WebApplication http://sharepoint -Limit All |
   Select -ExpandProperty AllWebs |
   Select -ExpandProperty Lists |
   Select ParentWebUrl, Title

I'm referencing http://sharepointpromag.com/sharepoint/windows-powershell-scripts-sharepoint-info-files-pagesweb-parts

All replies (6)

Thursday, July 10, 2014 11:06 PM | 2 votes

Have a look at below link

http://blog.morg.nl/2011/09/count-all-lists-and-list-items-in-a-site-collection/

$ListsInfo = @{}
$TotalItems = 0
$SiteCollection = Get-SPSite "http://<<SiteCollectionURL>>/"
ForEach ($Site in $SiteCollection.AllWebs)
{
    ForEach ($List in $Site.Lists)
    {
        $ListsInfo.Add($Site.Url + " - " + $List.Title, $List.ItemCount)
        $TotalItems += $List.ItemCount
    }
}
$ListsInfo.GetEnumerator() | sort name | Format-Table -Autosize
Write-Host "Total number of Lists: " $ListsInfo.Count
Write-Host "Total number of ListItems: " $TotalItems

My Blog- http://www.sharepoint-journey.com|
If a post answers your question, please click Mark As Answer on that post and Vote as Helpful


Friday, July 11, 2014 12:57 AM

 I ran the script and it returned with a message that read

"WARNING: column "Value" does not fit into the display and was removed."  It did however provide an output of total list items in the site collection, but that's not what I'm seeking.   I need table output with title, url and count for each list in the entire site collection. 

We have long urls in our environment, so I'm wondering if that's a factor on why the message is displayed. 


Friday, July 11, 2014 2:33 AM | 1 vote

You can export the output to CSV

#Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$ListsInfo = @{}
$TotalItems = 0
$SiteCollection = Get-SPSite "http://<<SiteCollectionURL>>/"
ForEach ($Site in $SiteCollection.AllWebs)
{
    ForEach ($List in $Site.Lists)
    {
        $ListURL = $Site.url +"/"+ $List.RootFolder.Url
        $ListsInfo.Add($List.Title + " - " + $ListURL, $List.ItemCount)
        $TotalItems += $List.ItemCount
    }
}
$ListsInfo.GetEnumerator() | sort name | Export-Csv c:\test.txt

My Blog- http://www.sharepoint-journey.com|
If a post answers your question, please click Mark As Answer on that post and Vote as Helpful


Friday, July 11, 2014 3:25 AM | 1 vote

Please find belwo script, it will iterarte through all the folder/Subfoder to get the item counts from all list and library, you can modify this script to run this at site collection scope:

Note: save the script in .ps1 file and execute the script as described below to get log file, it will save the log file out.txt in seleted directory:

e.g.PS D:\PowershellScripts> .\ListItemCount.ps1 > out.txt

$SPWebApp = Get-SPWebApplication "http://weburl.com/"

foreach ($SPSite in $SPWebApp.Sites)
{
    if ($SPSite -ne $null)
    {
        foreach ($SPWeb in $SPSite.AllWebs)
        {
            foreach ($list in $SPWeb.Lists)
            {
                
                    $ListURL = $SPWeb.url + "/" + $list.RootFolder.Url
                    Write-Output $ListURL
                    [Microsoft.SharePoint.SPQuery]$query = New-Object  Microsoft.SharePoint.SPQuery
#$query.Folder = fldr;
#Recursive Scope....
$query.ViewAttributes = "Scope='Recursive'"
$allitems = $list.GetItems($query);
$filecount = $allitems.Count;
                    Write-Output "  No of item: " $filecount

                    if ($SPWeb -ne $null)
                    {
                        $SPWeb.Dispose()
                    }
                
            }
        }
    }

    if ($SPSite -ne $null)
    {
        $SPSite.Dispose()
    }
}

You can update the code to get any specific list type item count, using if($list.BaseType -eq "DocumentLibrary") condition:

if($list.BaseType -eq "DocumentLibrary")
                {

                    $ListURL = $SPWeb.url + "/" + $list.RootFolder.Url
                    Write-Output $ListURL
                    [Microsoft.SharePoint.SPQuery]$query = New-Object  Microsoft.SharePoint.SPQuery
#$query.Folder = fldr;
#Recursive Scope....
$query.ViewAttributes = "Scope='Recursive'"
$allitems = $list.GetItems($query);
$filecount = $allitems.Count;
                    Write-Output "  No of item: " $filecount

                    if ($SPWeb -ne $null)
                    {
                        $SPWeb.Dispose()
                    }
                 }

If my contribution helps you, please click Mark As Answer on that post and Vote as Helpful

Thanks, ShankarSingh


Wednesday, July 19, 2017 6:51 PM

http://veerainduvasi.blogspot.co.uk/2017/07/get-site-item-count-for-sharepoint.html


Wednesday, May 22, 2019 7:09 AM

Hi Can we get the SPO Code too please