How to list all documents in a SharePoint site with Powershell

C
Collab365 TeamAuthorPublished Dec 23, 2016
3

At a Glance

Target Audience
SharePoint Administrators, IT Ops Managers
Problem Solved
Tedious manual auditing of documents across multiple SharePoint libraries and nested folders via UI.
Use Case
Full site document inventory for compliance audits, data cleanup, or migration prep.

Most admins try to audit SharePoint by clicking through folders. It never works.

You cannot manually check 50 libraries and expect to get an accurate count. Things get messy fast. You end up with hundreds of nested folders and thousands of misplaced files.

Stop trying to click your way out of a data problem. Your time is too valuable to spend tallying files by hand. You need to script the boring work.

I recently had to generate a full inventory of every document uploaded to a specific SharePoint site. Doing this through the UI is a nightmare. So I wrote a custom PowerShell function called Get-Documents.

It is not sexy. It is just a reliable process that loops through your environment and pulls exactly what you need.

The Inventory Script

Here is the exact code. Notice that the memory cleanup command sits outside the loop. If you put it inside, your script will crash after reading the first library.

function Get-Documents () {
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    $web = Get-SPWeb (Read-Host "Enter Site URL")

    foreach ($list in $web.Lists) {
        if ($list.BaseType -eq "DocumentLibrary") {
            foreach ($item in $list.Items) {
                $data = @{
                    "Web"           = $web.Url
                    "List"          = $list.Title
                    "Item ID"       = $item.ID
                    "Item URL"      = $item.Url
                    "Item Title"    = $item.Title
                    "Item Name"     = $item.Name
                    "Item Created"  = $item["Created"]
                    "Item Modified" = $item["Modified"]
                    "File Size"     = $item.File.Length / 1KB
                }
                New-Object PSObject -Property $data
            }
        }
    }
    $web.Dispose()
}

How It Works

Let us break down what is actually happening here.

First, the script loads the SharePoint Server on-premises object model. It prompts you for a site URL and then goes to work. If you ever struggle to load the required PowerShell snap-in, make sure you are running the management shell as an administrator.

The function ignores standard lists. It specifically targets document libraries using the $list.BaseType filter. Then it grabs the critical metadata for every single file.

You get the exact site-relative URL for each item. You get the creation dates. You get the file sizes.

Exporting Your Data

Once the function is loaded into your session, you have two ways to view your data.

You can push it directly to a grid view for quick sorting and filtering:
Get-Documents | Out-GridView

Or you can export it to a CSV file to hand off to your compliance team:
Get-Documents | Export-Csv -NoTypeInformation -Path C:\temp\inventory.csv

Focus on the data that matters. Let the script handle the heavy lifting.