title: Powershell script for purging files as per creation year
body: <p>I wrote a powershell script which will iterate through three different path and get list of files that are less then 7 years and delete them from current timestamp.
  I am getting creation year of file and if am able to recursively iterate through all those three path.
  Problem is out of 3, two paths have too many folders and files due to which when script is in loop it shows memory exception. Also I will not be able to set maxmemorypershellMB, since I don't have access.</p>
  
  <p>Anything else that I can do this to avoid memory exception
  this is piece of code below:</p>
  
  <pre><code>$files = Get-ChildItem "$path"  –Recurse -file    
  for ($i=0; $i -lt $files.Count; $i++) {
      $outfile = $files[$i].FullName                                #file name
      $FileDate = (Get-ChildItem $outfile).CreationTime                  #get creation date of file
      $creationYear = $FileDate.Year
      $creationMonth =$FileDate.Month                                     #get only year out of creation date
  
      If( $creationYear -lt $purgeYear ){
      If (Test-Path $outfile){                                           #check if file exist then only proceed
  
          $text=[string]$creationYear+" "+$outfile 
          $text &gt;&gt; 'listOfFilesToBeDeleted_PROD.txt'                          #this will get list of files to be deleted
           #remove-item $outfile 
              }
      }
  </code></pre>
  
  <p>}</p>