Recently my fellow SharePoint PFE and teammate Jim Matson (@jamesmatson3) was looking for a way to display the number of documents inside each document library on a SharePoint 2010 / 2013 farm. Below is the script that we came up with.
Problem
It is not too difficult to find all of the document libraries and their item count in a given SharePoint 2010 or 2013 site collection, web application, or farm. What can be difficult is finding all of that along with the full URL of the site that contains the list and exporting in one object..
Solution
I used a trick I learned from my previous post on getting Exchange Online mailbox size in GB to use the add-member commandlet to add metadata to a variable or object. By gathering the site URL and then adding it to a variable of the document library title and item count I had the information Jim needed. At the end simply output this to a CSV file and then use as needed. Feel free to download the script from my SkyDrive public folder link below.
Note: I am not testing to see if the output folder exists. Handle that as you wish if you use this script.
Add-PSSnapin Microsoft.SharePoint.PowerShell Start-SPAssignment -Global $OutputFile = “C:tempDocCount.csv” $results = @() $webApps = Get-SPWebApplication foreach($webApp in $webApps) { foreach($siteColl in $webApp.Sites) { foreach($web in $siteColl.AllWebs) { $webUrl = $web.url $docLibs = $web.Lists | Where-Object {$_.baseType -eq “DocumentLibrary”} $docLibs | Add-Member -MemberType ScriptProperty -Name WebUrl -Value {$webUrl} $results += ($docLibs | Select-Object -Property WebUrl, Title, ItemCount) } } } $results | Export-Csv -Path $OutputFile -NoTypeInformation Stop-SPAssignment -Global
Here is an example screenshot of the output from the script.
Conclusion
This script was fairly easy to come up with. I was happy to be able to combine some previous knowledge learned from another script along with traversing all sites in a farm. Hopefully if you have need of this script (or something similar) you will find this helpful.
-Frog Out
Originally posted on: https://briantjackett.com/archive/2013/06/05/powershell-script-to-determine-number-of-files-in-sharepoint-2010.aspx#637160HelloGreat script! I have been trying for a couple of days to alter the code to show me Lists rather than Document Libraries (or even ALL would be fine!!), however, when I change the Where clause to “GenericLists” rather than “DocumentLibraries” the script errors out. When I remove the where clause altogether and just have $docLibs = $web.lists, it does go through all lists (and maybe libraries?), but the WebURL is blank in the output file. Naturally, that is one of the key pieces of information for analyzing large lists and libraries.Any way you could shed some light? I’d very much appreciate it!
LikeLike
Originally posted on: https://briantjackett.com/archive/2013/06/05/powershell-script-to-determine-number-of-files-in-sharepoint-2010.aspx#642715A library *is* a list. So maybe just try changingbaseType -eq “DocumentLibrary”to -ne instead of -eq. Or remove the clause altogether if you want both normal lists as well as libraries.
LikeLike
Originally posted on: https://briantjackett.com/archive/2013/06/05/powershell-script-to-determine-number-of-files-in-sharepoint-2010.aspx#644938I get an error trying to run this script: Exception has been thrown by the target of an invocation.any thoughts?
LikeLike
Originally posted on: https://briantjackett.com/archive/2013/06/05/powershell-script-to-determine-number-of-files-in-sharepoint-2010.aspx#646953Is there anyway to display the total number of documents for each URL separately from the number of documents in each of the sites libraries?
LikeLike
Originally posted on: https://briantjackett.com/archive/2013/06/05/powershell-script-to-determine-number-of-files-in-sharepoint-2010.aspx#649173Is there a way to filter the results by date? For example, only show items that were created within the last month.
LikeLike
Originally posted on: https://briantjackett.com/archive/2013/06/05/powershell-script-to-determine-number-of-files-in-sharepoint-2010.aspx#649927Hi Thanks a lot for the program. how can we modify the above program to get the same results for 6 months ago and 12 months ago. So that we can analyse the documents number growth trend.
LikeLike