PowerShell script to obtain the most viewed document in SharePoint folder, document library and site

Nishanth Chakkere Ramesh 400 Reputation points
2025-06-18T21:18:16.1866667+00:00

Hi,

What is the PowerShell script to obtain the most viewed document in a SharePoint folder, document library and site?

Thanks,

Nishanth Chakkere

Microsoft 365 and Office SharePoint Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Teddie-D 1,030 Reputation points Microsoft External Staff Moderator
    2025-06-19T05:12:35.0033333+00:00

    Hi @Nishanth Chakkere Ramesh, 

    Thank you for posting your question in the Microsoft Q&A forum. 

    Based on my research, there isn't a PowerShell script available to retrieve the most viewed document in a SharePoint site. 

    Fortunately, after testing, I have found a possible method using PowerShell through PnP Online. Please follow the instructions in this article How to connect to PnP Online with Interactive Authentication to connect PnP PowerShell in your PowerShell environment. 
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link. 
    After completing PnP configuration, run these below commands in PS (You should use PowerShell 7.5 for a more effective configuration) 

    #Install  
    Import-Module PnP.PowerShell 
    # Connect to SharePoint site  
    Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive –ClientID <your application ID>    
    #Execute Search  
    $SiteURL = "https://yourtenant.sharepoint.com/sites/yoursite/Shared%20Documents" 
    $SearchQuery = "* Path:" + $SiteURL 
      
    $SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All -TrimDuplicates $False -SelectProperties Filename, Author, Size, ListItemID, LastModifiedTime, ViewsLifeTime 
      
    #Collect Data from search results 
    $Results = @() 
    ForEach ($ResultRow in $SearchResults.ResultRows)  
    {      
        $Results += [pscustomobject] @{ 
            Filename   = $ResultRow["Filename"] 
            Author = $ResultRow["Author"] 
            Size = $ResultRow["Size"] 
            LastModified = $ResultRow["LastModifiedTime"] 
            ListItemID     = $ResultRow["ListItemID"] 
            ParentFolder = $ResultRow["ParentLink"]          
            ViewsLifeTime = $ResultRow["ViewsLifeTime"]        
            URL       = $ResultRow["Path"] 
        }  
    } 
    $Results 
     
    # Sort by ViewsLifeTime in descending order and select the top item 
    $TopViewed = $Results | Sort-Object ViewsLifeTime -Descending | Select-Object -First 1 
      
    # Export or display the file with the most views 
    $TopViewed | Select-Object Filename, ViewsLifeTime
    
    

    I hope this helps! If you have any other questions, feel free to ask.


    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 


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.