I got an email today asking if I had anything that would generate a report detailing all the documents throughout an entire SharePoint Farm. As this wasn’t the first time I’ve been asked this same question I decided that I’d just go ahead and post the script for generating such a report.

The script is really quite straightforward – it simply iterates through all Web Applications, Site Collections, Webs, Lists, and finally, List Items. I skip any List that is not a Document Library (as well as the Central Admin site) and then build a hash table containing all the data I want to capture. I then convert that hash table to an object which is written to the pipeline.

All of this is placed in a function which I can call and then pipe the output to something like the Out-GridView cmdlet or the Export-Csv cmdlet. I also wrote the script so that it works with either SharePoint 2007 or SharePoint 2010 so that I don’t have to maintain two versions (I could have used cmdlets such as Get-SPWebApplication, Get-SPSite, and Get-SPWeb but there was little benefit to doing so and the script would be limited to SharePoint 2010).

One word of caution – in a large Farm this script should be run off hours or at least on a back facing server (not your WFE) – it’s going to generate a lot of traffic to your database.

 1function Get-DocInventory() {
 2    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 3    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
 4    foreach ($spService in $farm.Services) {
 5        if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
 6            continue;
 7        }
 8
 9        foreach ($webApp in $spService.WebApplications) {
10            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }
11
12            foreach ($site in $webApp.Sites) {
13                foreach ($web in $site.AllWebs) {
14                    foreach ($list in $web.Lists) {
15                        if ($list.BaseType -ne "DocumentLibrary") {
16                            continue
17                        }
18                        foreach ($item in $list.Items) {
19                            $data = @{
20                                "Web Application" = $webApp.ToString()
21                                "Site" = $site.Url
22                                "Web" = $web.Url
23                                "list" = $list.Title
24                                "Item ID" = $item.ID
25                                "Item URL" = $item.Url
26                                "Item Title" = $item.Title
27                                "Item Created" = $item["Created"]
28                                "Item Modified" = $item["Modified"]
29                                "File Size" = $item.File.Length/1KB
30                            }
31                            New-Object PSObject -Property $data
32                        }
33                    }
34                    $web.Dispose();
35                }
36                $site.Dispose()
37            }
38        }
39    }
40}
41Get-DocInventory | Out-GridView
42#Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\inventory.csv