powershellsharepointsharepoint-onlinesharepoint-listpnp-js

How to get item ID of the last modified item in the SharePoint online list using pnp powershell?


I am able to get the date of the last modified item in the list using the script below:

$SiteURL="https://abc.sharepoint.com/sites/sitename"
$ListName="Documents"

Connect-PnPOnline -Url $SiteURL -Credentials (Get Credentials)

(Get-PnPList -Identity $ListName).LastItemUserModifiedDate

Please help me with getting the Item ID of the above last modified item.


Solution

  • Order by Modified field with desc order and get the first item of the returned items collection:

    $SiteURL= "https://Tenant.sharepoint.com/"
    $ListName = "Documents"
     
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    #Define Query to Filter
    $Query= "<View Scope='RecursiveAll'>
                <Query>
                    <Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Eq></Where>
                    <OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy>
                    </Query>
            </View>"
     
    $ListItems = Get-PnPListItem -List $ListName -Query $Query
    
    Write-host "Last Modified Item Id:"$ListItems[0].Id