Find all documents that are created more than four years old (based on created date)

Hi,

I am looking to write a Powershell script that can do the following:

  1. Find all documents that are created more than four years old (based on created date)
  2. For each of these document sets, find the oldest docuemnt (even if in a subfolder), and if the oldest document has not been modified in four years then set the document set metadata 'Ready to Archive' to 'Yes'.

The script should be running against a site collection and all webs within that Site Collection.

Thanks,

AB

July 20th, 2015 8:58pm

I have written this powershell script and it works perfectly :)

#Declare variables
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.DocumentManagement")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$Url = ''  #specify the Url
$Url = $Url.Replace("'", "")
$site = new-object Microsoft.SharePoint.SPSite($Url)
# set minimum age of files and folders
$min_years = "-4"
$StartDate=(GET-DATE)

Function GetMyFiles($Folder)
{
Write-Host +in : $Folder.Name
foreach($file in $Folder.Files)
{
$datemodified = $file.TimeLastModified
# determine how far back we go based on current date
$archivedate = $StartDate.AddYears($min_years)
if($datemodified -lt $archivedate)
{
#Find all documents Set that are more than four years old based on Date Created
# get the current date
$readytoarchive = "Yes"
write-Host `t $file.Name $datemodified "--DateModified " $readytoarchive "--ReadyToArchive"
}
else
{
$readytoarchive = "No"
}
}
if($readytoarchive -eq "Yes")
{
Write-Host $Folder.Properties["vti_title"] " is ready to archive....."
$Folder.Properties["ReadyToArchive"] = $readytoarchive
$Folder.Update()
}
}

Write-Host $site -ForegroundColor Blue;

foreach ($web in $site.AllWebs)
{

Write-Host $web -ForegroundColor Cyan;

foreach ($list in $web.Lists)
{
foreach ($contenttype in $list.contenttypes)
{
if($contenttype.name -eq "Legal Matter Document Set"){ # If list has content type as Legal Matter Document Set
$rootFolder = $list.RootFolder
foreach($documentsetfolder in $rootFolder.SubFolders) {
if($documentsetfolder.Properties['vti_progid'] -eq "Sharepoint.DocumentSet")
{
$datecreated = $documentsetfolder.Properties['vti_timecreated']
# determine how far back we go based on current date
$archivedate = $StartDate.AddDays($min_years)
if($datecreated -lt $archivedate)
{
#Find all documents Set that are more than four years old based on Date Created
# get the current date
Write-Host $documentsetfolder.Item.Title "--DocumentSet " $datecreated "--DateCreated"
foreach($subfolder in $documentsetfolder.Subfolders)
{
if($SubFolder.Name -ne Forms)
{
GetMyFiles($subfolder)
}
}
GetMyFiles($documentsetfolder)
}
}
}
}
}


} # end of for-each loop for document library
} # end of for-each loop for all webs

$web.Dispose()
$site.Dispose() 

  • Marked as answer by Aammiitt22 2 hours 10 minutes ago
July 21st, 2015 1:28am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics