Get 5 Most Recently Update Items From ALL Lists in a FARM

A wise man recently asked me how I’d go about retrieving the five most recent modified items from every list in a farm. Fun question and there are a couple of ways of going about this, but here is what I came up with.

Things to note: 1) on prem farm with more than one web app. 2) if you are dealing with a large farm, I’d suggest chunking out the Get-SPSite -limit all and the $site.allwebs into to smaller data sets. 3) script needs comments and error handling or reporting


if ($null -eq (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue)) {
    Add-PsSnapin Microsoft.SharePoint.PowerShell

$sites = Get-SPWebApplication | Get-SPSite -limit all 

foreach ($site in $sites) {
    foreach ($web in $site.AllWebs) {
        $lists = $web.Lists
        foreach ($list in $lists) {
            $query = New-Object Microsoft.SharePoint.SPQuery 
            $query.Query =  
                  <FieldRef Name='Modified' Ascending='False' />
            $query.RowLimit = 5
            $listItems = $list.GetItems($query)
            if ($listItems) {
                foreach ($lIem in $listItems) {
                    Write-Host "Web: $web -- List: $list -- Item: $($lIem.Name) "
        if ($web) {

Leave a Reply

Your email address will not be published. Required fields are marked *