Home Contact

The Frog Pond of Technology

Ripples of Knowledge for SharePoint and Other .Net Technologies

News

My blog has moved to WordPress at BrianTJackett.com. Any pages will automatically redirect to the new address in 7 seconds. Please excuse an issues during this migration.

 Subscribe to this blog


About Me

Name:
Brian T. Jackett
Location:
Columbus, OH
Company:
Microsoft

Find me on...

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

Syndication:

PowerShell Script to Determine Number of Files in SharePoint 2010 or 2013 Document Libraries

   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.

 

 

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028

Add-PSSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global

$OutputFile = "C:\temp\DocCount.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.

NumberDocsInSPDocLib1

 

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


Wednesday, June 5, 2013 4:37 PM

Feedback

# re: PowerShell Script to Determine Number of Files in SharePoint 2010 or 2013 Document Libraries

Hello
Great 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! 4/16/2014 5:19 PM | Brian

# re: PowerShell Script to Determine Number of Files in SharePoint 2010 or 2013 Document Libraries

A library *is* a list. So maybe just try changing

baseType -eq "DocumentLibrary"

to -ne instead of -eq. Or remove the clause altogether if you want both normal lists as well as libraries. 1/27/2015 12:35 PM | Rob Schifreen

# re: PowerShell Script to Determine Number of Files in SharePoint 2010 or 2013 Document Libraries

I get an error trying to run this script:

Exception has been thrown by the target of an invocation.

any thoughts? 6/24/2015 10:37 AM | Andy

# re: PowerShell Script to Determine Number of Files in SharePoint 2010 or 2013 Document Libraries

Is there anyway to display the total number of documents for each URL separately from the number of documents in each of the sites libraries? 11/6/2015 6:27 PM | Pinar

# Filter by date

Is there a way to filter the results by date? For example, only show items that were created within the last month. 4/8/2016 4:18 PM | Ken

# re: PowerShell Script to Determine Number of Files in SharePoint 2010 or 2013 Document Libraries

Hi 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.
5/20/2016 11:13 AM | raj

Post A Comment
Title:
Name:
Email:
Comment:
Verification: