I recently needed to migrate all documents that hadn’t been modified in 90 days in all SharePoint webs within a SharePoint site collection to a records center.  There could potentially be hundreds of libraries and content types and creating Information Management Policies at this point wasn’t really feasible.  With some good upfront planning, that would have been the preferable method, but this was the quick and easy way to accomplish the task.

This script takes two URLs as parameters and the number of days to compare against the last modified date.  The two URLs are the root of the site collection you want to archive documents in, and the other is the path to the destination library where the documents will be moved.

This will loop through all libraries in all webs in a site collection and look for documents whose last modified date is older than 90 days.  I then set this up using a scheduled task in Windows Task Scheduler to run on a recurring basis to keep documents in the correct location.

   1: #Add SharePoint PowerShell SnapIn if not already added
   2: if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
   3:    Add-PSSnapin "Microsoft.SharePoint.PowerShell" }
   4:  
   5:  
   6: $siteURL = "http://sitecollectionroot"
   7: $destinationURL = "http://path/to/destination/web/including/library"
   8:  
   9: $numDaysToExpire = 90
  10: $expireDate = Get-Date
  11: $expireDate = $expireDate.AddDays(-$numDaysToExpire)
  12:  
  13: Get-SPSite -Identity $siteURL | Get-SPWeb -limit all | foreach {
  14:    write-host "Working on Web: "$_.Title -ForegroundColor Green
  15:    foreach ($list in $_.Lists){
  16:        if($list.BaseType -eq "DocumentLibrary" -and $List.Hidden -eq $false -and $list.Title -ne "Style Library" -and $list.Title -ne "Master Page Gallery" -and $list.Title -ne "List Template Gallery" -and $list.Title -ne "Theme Gallery" -and $list.Title -ne "Web Part Gallery") {
  17:            write-host "   Working on library: "$list.Title -ForegroundColor Cyan
  18:            foreach($item in $list.Items){
  19:                if($item -lt $expireDate){
  20:                    write-host "       Moving $($item) with modified date $($item)"
  21:                    $item.File.MoveTo($destinationURL + "/" + $item.Name, $true)
  22:                }
  23:            }
  24:        }
  25:    } }

You can also get fancy if you want and automatically create folders in the destination based on where it came from.  To create a SharePoint folder using PowerShell, you can use this code.  This checks to see if the folder exists, and if not, creates it based on the name of the web.


   1: $folderName = $destination.Folders | where {$_.Name -eq $_}
   2:     
   3: if(!$folderName)
   4: {
   5:     $newFolder = $destination.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$_)
   6:     $newFolder.Update()
   7:     $folderName = $destination.Folders | where {$_.Name -eq $_}
   8: }

For more information on C5 Insight or this blog, please Contact Us