I have managed to write a Powershell but $documentsetfolder.Files is not getting into subfolders in the documentset. Is there a way I can traverse through files in subfolders without using foreach loop due to performance cost.
#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"
$oldest = $documentsetfolder.Files | Sort-Object -Property TimeLastModified -Verbose -Descending | Select-Object -Last 1
***************************************************************************************
#Complete Script from here
#Declare variables
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.DocumentManagement")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$Url = ''
$Url = $Url.Replace("'", "")
$site = new-object Microsoft.SharePoint.SPSite($Url)
# set minimum age of files and folders
$min_years = "-4"
$StartDate=(GET-DATE)
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.AddYears($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"
$oldest = $documentsetfolder.Files | Sort-Object -Property TimeLastModified -Verbose -Descending | Select-Object -Last 1
Write-Host +in : $oldest.Name
$datemodified = $oldest.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 $documentsetfolder.Properties["vti_title"] " is ready to archive....."
$documentsetfolder.Properties["ReadyToArchive"] = $readytoarchive
$documentsetfolder.Update()
}
}
}
}
}
}
} # end of for-each loop for document library
} # end of for-each loop for all webs
$web.Dispose()
$site.Dispose()