0

My colleague and I inherited an unwieldy SharePoint (SharePoint Online) site and I've been tasked with implementing better organization.

The first step is to figure out what all is in the site - ideally I'd like to create a site map, but even a document list would help. I'm here asking if anyone has a shortcut and/or some code that they've used for this purpose in the past.

We've tried the file report tool that SharePoint has, but it was a bust. We also tried the script I found here How to list all documents in a SharePoint site with PowerShell, but it also failed to give a complete list.

1 Answer 1

0

Here is the PnP PowerShell way to get all files from all document libraries of a given site collection to a CSV file.

#Parameters
$SiteURL = "https://crescent.SharePoint.com/sites/Marketing"
$CSVPath = "C:\Temp\DocumentInventory.csv"
$global:DocumentInventory = @()
$Pagesize = 2000
 
#Function to scan and collect Document Inventory
Function Get-DocumentInventory
{
     [cmdletbinding()]
    param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Web)
   
    Write-host "Getting Documents Inventory from Site '$($Web.URL)'" -f Yellow
    #Calculate the URL of the tenant
    If($Web.ServerRelativeUrl -eq "/")
    {
        $TenantURL = $Web.Url
    }
    Else
    {
        $TenantURL = $Web.Url.Replace($Web.ServerRelativeUrl,'')
    }
 
    #Exclude certain libraries
    $ExcludedLists = @("Form Templates", "Preservation Hold Library","Site Assets", "Pages", "Site Pages", "Images",
                            "Site Collection Documents", "Site Collection Images","Style Library")
                               
    #Get All Document Libraries from the Web
    Get-PnPList -Web $Web -PipelineVariable List | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $ExcludedLists -and $_.ItemCount -gt 0} | ForEach-Object {
        #Get Items from List  
        $global:counter = 0;
        $ListItems = Get-PnPListItem -List $_ -Web $web -PageSize $Pagesize -Fields Author, Created, File_x0020_Type -ScriptBlock `
                 { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($_.ItemCount) * 100) -Activity "Getting Documents from '$($_.Title)'" -Status "Processing Items $global:Counter to $($_.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
        Write-Progress -Activity "Completed Retrieving Documents from Library $($List.Title)" -Completed
     
            #Get Root folder of the List
            $Folder = Get-PnPProperty -ClientObject $_ -Property RootFolder
 
            #Iterate through each document and collect data          
            ForEach($ListItem in $ListItems)
            { 
                #Collect document data
                $global:DocumentInventory += New-Object PSObject -Property ([ordered]@{
                    SiteName  = $Web.Title
                    SiteURL  = $Web.URL
                    LibraryName = $List.Title
                    ParentFolder = $Folder.ServerRelativeURL
                    FileName = $ListItem.FieldValues.FileLeafRef
                    FileType = $ListItem.FieldValues.File_x0020_Type
                    AbsoluteURL = "$TenantURL$($ListItem.FieldValues.FileRef)"
                    CreatedBy = $ListItem.FieldValues.Author.Email
                    CreatedAt = $ListItem.FieldValues.Created
                    ModifiedBy = $ListItem.FieldValues.Editor.Email
                    ModifiedAt = $ListItem.FieldValues.Modified
                })
            }
        }
}
  
#Connect to Site collection
Connect-PnPOnline -Url $SiteURL -UseWebLogin
    
#Call the Function for Web & all Subwebs
Get-PnPWeb | Get-DocumentInventory
Get-PnPSubWebs -Recurse| ForEach-Object { Get-DocumentInventory $_ }
Disconnect-PnPOnline
   
#Export Documents Inventory to CSV
$Global:DocumentInventory | Export-Csv $CSVPath -NoTypeInformation
Write-host "Documents Inventory Report has been Exported to '$CSVPath'"  -f Green 

More information:

https://www.sharepointdiary.com/2018/03/sharepoint-online-get-all-documents-using-powershell.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.