Get All Files from SharePoint Document Library using PnP PowerShell

In this article, we will explore how to retrieve all file documents and their metadata details from a Document Library in SharePoint Online or OneDrive for Business site. In SharePoint, the files are represented as a List item under the Document Library List. We can use the PnP PowerShell command Get-PnPListItem to get all list items from the given List/Document library.

Install the latest PnP PowerShell module if you have already not installed it. Using the below script, you can generate the following files inventory reports.

Export All Files from SharePoint Online Library using PnP Powershell

The below script retrieves all the file documents from the given site document library and exports the file metadata details (such as file name, file relative path, file size, file created time, last modified time, author name, and last modified user email) to a CSV file.

$AllFiles = @() # Result array to keep all file details
  
#Specify SharePoint Online Site URL or User's OneDrive Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"

#Provide name of your List/Document library
$DocumentLibrary = "Documents"
  
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL
 
#Retrieve all Files from the document library
$ListItems = Get-PnPListItem -List $DocumentLibrary -PageSize 1000 | Where {$_["FileLeafRef"] -like "*.*"}
  
#Enumerate all list items to get file details
ForEach($Item in $ListItems)
{
#Add file details to Result array
$AllFiles += New-Object PSObject -property $([ordered]@{ 
FileName  = $Item.FieldValues["FileLeafRef"]            
FileID = $Item.FieldValues["UniqueId"]
FileType = $Item.FieldValues["File_x0020_Type"]
RelativeURL = $Item.FieldValues["FileRef"]
CreatedByEmail = $Item.FieldValues["Author"].Email
CreatedTime   = $Item.FieldValues["Created"]
LastModifiedTime   = $Item.FieldValues["Modified"]
ModifiedByEmail  = $Item.FieldValues["Editor"].Email
FileSize_KB = [Math]::Round(($Item.FieldValues["File_x0020_Size"]/1024), 2) #File size in KB
})

}
$AllFiles | Export-CSV "C:\SharePoint-Files.CSV" -NoTypeInformation -Encoding UTF8
Write-Host "SharePoint files report exported successfully" –f Green

Find recently uploaded/created documents

The above script store all files details in the array object “$AllFiles“, we can generate the required report from this result array. Run the below commands to list the recently uploaded (or) created files.

$Days = 30 #No of days - Get files created in last 30 days
$time = (Get-Date).Adddays(-($Days))
$AllFiles | Where-Object { $_.CreatedTime -gt  $time}

Find recently modified documents

Run the following commands to list the recently updated (or) modified files.

$Days = 30 #No of days - Get files edited in last 30 days
$time = (Get-Date).Adddays(-($Days))
$AllFiles | Where-Object { $_.LastModifiedTime  -gt  $time}

Find files created by a specific user

The below command retrieves file documents that are uploaded by a specific user.

$userEmail = "[email protected]"
$AllFiles | Where-Object { $_.CreatedByEmail -like $userEmail }

Find files modified by a specific user

Run the following command to get files that are updated by a specific user.

$userEmail = "[email protected]"
$AllFiles | Where-Object { $_.ModifiedByEmail -like $userEmail }
Advertisement