You can do this by using these options
- Enable Auditing:
To start tracking file access events and find the last consultation date, you'll need SharePoint administrator or global administrator permissions. Follow these steps:
- Go to your SharePoint Online site and click the "Settings" gear icon in the top right corner.
- Choose "Site settings" from the menu.
- Under "Site Collection Administration," click on "Site collection audit settings."
- Enable the "Opening or downloading documents, viewing items in lists, or viewing item properties" option under "Documents and Items."
Configure audit data for a site collection - Microsoft Support
- Wait for Auditing Data:
Once you've enabled auditing, SharePoint Online will start collecting audit data. Keep in mind that it might take some time before the audit data becomes available for use.
- Use PowerShell to Get Audit Data:
you can use PowerShell along with the SharePoint PnP module to retrieve the last accessed date of files. If you haven't installed the SharePoint PnP module, you can do it with this simple command:
Install-Module SharePointPnPPowerShellOnline -Force
- Connect to SharePoint Online Site
Connect to your SharePoint Online site using PnP PowerShell. Replace'[https://your-sharepoint-site](https://your-sharepoint-site/)'
with your actual SharePoint site URL. You'll be prompted to provide your credentials:
Connect-PnPOnline -Url "https://your-sharepoint-site" -UseWebLogin
- Retrieve Last Consultation Dates:
retrieve the audit log data and filter for file access events to get the last consultation date for each file in your chosen library. Make sure to replace'/sites/YourSite/LibraryName/'
with the correct path to the document library you want to track:
$libraryUrl = '/sites/YourSite/LibraryName/'$auditData = Get-PnPAuditLog -Query "<Query><Where><And><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$libraryUrl</Value></Eq><Eq><FieldRef Name='Event' /><Value Type='String'>View</Value></Eq></And></Where></Query>"
foreach ($entry in $auditData) {$fileUrl = $entry.ItemUrl$lastAccessedDate = $entry.OccurredWrite-Host "File URL: $fileUrl, Last Consultation Date: $lastAccessedDate"}
When the First part of the script doesn't work, install SharePoint Online Management Shell, and run Powershell commands as an admin in SharePoint Online Management Shell.
Download SharePoint Online Management Shell from Official Microsoft Download Center
***If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards
Cheng Feng