Retrieve Likes & Comments count from SharePoint Online pages using PowerShell PnP

Today, we’re going to play a bit with SharePoint Online pages, and try to get the number of Likes & Comments we have for each page within a Site Collection.
Without further due, let’s get started!


Retrieve Likes & Comments count for a specific page

First, let’s try to get the counts for only one page.
I have created a page from the News webpart, and at the beginning, this is what you’d see:

ℹ️ Pages are located under Site Pages, and this page is called PnP-Demo.aspx


There’s no likes or comments yet (assuming you’ve enabled the comments!)
So let’s run some PowerShell to confirm that 😉

⚠️ I may go a bit in detail….. But hey ho!

# Connect to SPO site
Connect-PnPOnline -Url "https://<TENANT-NAME><YOUR-SITE>

# Store in a variable the Site Pages gallery
$sitePagesGallery = Get-PnPList -Identity "Site Pages"

At this stage, this is what the $sitePagesGallery variable will show us:


Now, if we want to see all the pages within this gallery, run Get-PnPListItem -List $sitePagesGallery
In my case, I have 2 pages only.

💡 If you want to know how many pages on the site, you can run $sitePagesGallery.ItemCount


In the next step, we’ll isolate this PnP Demo page, by storing it in a variable like so : $PnPDemoPage = Get-PnPListItem -List $sitePagesGallery | Where-Object {$_.Id -eq 3}


And the final step (😇) is to get this likes & comments count!
Run $PnPDemoPage.FieldValues and scroll to the _CommentCount and _LikeCount properties!


Currently we have nothing to show, but let’s change that………. Abracadabra…. ✨



Retrieve Likes & Comments count for all pages in a site

We’re not going to start over, but use the variables we already have.
However, this is the full script for your convenience 😉 Simply change the tenant name & site to yours!


#Connect to SPO site
Connect-PnPOnline -Url https://<TENANT-NAME><YOUR-SITE>

#Store pages into a variable
$sitePagesGallery = Get-PnPList -Identity "Site Pages"

#Get the pages details 
$logFile = "C:\users\$env:USERNAME\Desktop\LikesAndComments.csv"
$results = @()

foreach ($item in $sitePagesGallery) {
    $allPages = Get-PnPListItem -List $sitePagesGallery -Fields "FileLeafRef", "Title", "ID", "FileRef", "_CommentCount", "_LikeCount"
    #Choose the properties to export
    foreach ($page in $allPages) {
        $results += New-Object -TypeName psobject -Property @{
            Title         = $page["Title"]
            ID            = $page.ID
            FullPath      = $page["FileRef"]
            NumOfComments = $page["_CommentCount"]
            NumOfLikes    = $page["_LikeCount"]        
$results | Export-Csv -Path $logFile -NoTypeInformation


Et voilà!





2 Responses to “Retrieve Likes & Comments count from SharePoint Online pages using PowerShell PnP

  • SPhunter
    2 years ago

    Can you help us in getting page views as well

      1 year ago

      I have the same need. HOw to get the views please ?

%d bloggers like this: