1

I need to get a list of all files, older then 5 years, from few thousands sharepoint online sites and I am trying to do it with powershell script. The problem I am having is that I have to reconnect with Disconnect/Connect-PnPOnline for each site which causes server to stop responding after few hundreds sites.

foreach($site in $sites){
 $surl = $site.SiteUrl ; $sid = $site.ID
 $gcon =  Connect-PnPOnline -Url $surl -ClientId $GLClientId -ClientSecret $GLSecret -ReturnConnection  # Connection via Global SP app to read current site
 Get-PnPListItem -List $folder  -PageSize 500 -Connection $gcon | ForEach-Object{
   if($_.FileSystemObjectType -like "*File*"){        $f=$_; 
        if($t_TooOldDate -gt $f.FieldValues.Last_x0020_Modified){ ### 
             $a_files+=[pscustomobject]@{'File'=$f.FieldValues.FileRef; 'Size'= $f.FieldValues.File_x0020_Size ; 'LastModified'=$f.FieldValues.Last_x0020_Modified}
             $co++ ; $ts+= $f.FieldValues.File_x0020_Size
          } 
       }
    }
  Disconnect-PnPOnline -Connection $gcon
}

So I am wandering if I could use same connection for the whole script and just keep changing site name? Or maybe there is a better way to do it?

Thanks

2
  • 1
    After conducting my investigation and reviewing my previous response, it appears that traversing with the same connection across different site collections is not possible. You ll have to use Connect-PnPOnline again.
    – Pradip R.
    Commented May 16, 2023 at 8:16
  • Try use connection variable - i.e. Connect-PnPOnline with the option "-ReturnConnection". I work with 10K+ sites environment - and I was able to iterate through all sites and connect to every site... I used clientid and clientsecret. Commented Jun 8, 2023 at 21:44

1 Answer 1

0

You can use MS Graph API to get list of sites sorted/filtered based on Last Modified Date. Here is how I get new sites, but the same technique could be used to get older sites.

If you prefer PnP.PowerShell - Try connection variable. I administer large environment and I was able to iterate through all sites and connect to every site...

$conn = Connect-PnPOnline -ClientId $clientid -Url $url -ClientSecret $clientSecret -ReturnConnection
$site = Get-PnPSite -Connection $conn

Also, just FYI: SPO Site LastContentModifiedDate vs LastItemModifiedDate vs LastItemUserModifiedDate vs Graph LastModifiedDateTime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.