title: PowerShell foreach file in folder using a lot of memory
body: <p>I'm fairly new to PowerShell and programming, so I'm sure this code can be optimized even more. </p>
  
  <p>However, my biggest problem with this code is that it puts it's data from the foreach loop in memory. What is the best way to prevent this?</p>
  
  <p>I started up rewriting this script from a script that I found here: <a href="http://www.networknet.nl/apps/wp/published/powershell-delete-files-older-than-x-days" rel="nofollow" title="PowerShell: delete files older than x days">PowerShell: delete files older than x days</a></p>
  
  <p>Edit: Oh, there is about 60 000~ files in each TargetFolder by the way.</p>
  
  <pre><code>## Set Window Title
  $title = $Host.UI.RawUI.WindowTitle
  $titdef = $title
  $Host.UI.RawUI.WindowTitle = "Checking files. Do NOT close " + "($title)"
  
  ## Debug (0=Nothing, 1=Extended log, 2=Extended log+Screen)
  $Debug = "2"
  
  ## Log file
  $log = "C:\status\status.txt"
  Add-Content $log "nul"
  
  ## Clear log before adding new data 
  Remove-Item $log
  
  ## Folders to check
  $TargetFolder1 = “\\server\c$\BiztalkProjects\UDC\output\ume”
  $TargetFolder2 = “\\server\c$\BiztalkProjects\UDC\output\upp”
  
  ## Required stuff
  $Now = Get-Date
  $Days = “2”
  $TwoDays = $Now.AddDays(-$Days)
  $Folder1 = get-childitem $TargetFolder1 -include *
  $Folder2 = get-childitem $TargetFolder2 -include *
  
  ## Window Message
  "Checking for files newer than $Days days in:"
  "$TargetFolder1"
  "$TargetFolder2"
  ""
  "This may take 5-15 minutes . . . (Will commit about 250MB~ RAM during the period)"
  
  ## Reset variable $OK to "0"
  $OK = 0
  
  if ($Debug -eq "2") {"Check1"}
  foreach ($File in $Folder1) {
      $FileLastWrite = $File.LastWriteTime
      if ($File.LastWriteTime -le $TwoDays) {
          $OK = $OK + "0"
          if ($Debug -eq "2") {
              "OK + 0 (=$OK) $File ($FileLastWrite)"
              }
      } else {
          $OK = $OK + "1"
          if ($Debug -eq "2") {
              "OK + 1 (=$OK) $File ($FileLastWrite)" 
              }
      }
  }
  if ($Debug -eq "2") {"Check2"}
  foreach ($File in $Folder2) {
      $FileLastWrite = $File.LastWriteTime
      if ($File.LastWriteTime -le $TwoDays) {
          $OK = $OK + "0"
          if ($Debug -eq "2") {
              "OK + 0 (=$OK) $File ($FileLastWrite)"
              }
      } else {
          $OK = $OK + "1"
          if ($Debug -eq "2") {
              "OK + 1 (=$OK) $File ($FileLastWrite)" 
              }
      }
  }
  if ($OK -gt "0") {
      if ($Debug -eq "2") {
          ""
          "Found $OK file(s) newer than $Days days"
          }
      Add-Content $log "OK"
      if ($Debug -ge "1") {
          Add-Content $log "Found $OK file(s) newer than $Days days"
          }
      }
      else {
          if ($Debug -eq "2") {
          ""
          "Found $OK file(s) newer than $Days days"
          }
      Add-Content $log "Error"
      if ($Debug -ge "1") {
          Add-Content $log "Found $OK file(s) newer than $Days days"
          }
      }
  $Host.UI.RawUI.WindowTitle = $titdef
  </code></pre>
  
  <p>This version works. Thanks to all who helped! </p>
  
  <pre><code>## Set Window Title
  $title = $Host.UI.RawUI.WindowTitle
  $titdef = $title
  $Host.UI.RawUI.WindowTitle = "Checking files. Do NOT close " + "($title)"
  
  ## Debug (0=Nothing, 1=Extended log, 2=Extended log+Screen)
  $Debug = "2"
  
  ## Log file
  $log = "C:\status\status.txt"
  Add-Content $log "nul"
  
  ## Clear log before adding new data 
  Remove-Item $log
  
  ## Folders to check
  $TargetFolder1 = “\\server\c$\BiztalkProjects\UDC\output\ume”
  $TargetFolder2 = “\\server\c$\BiztalkProjects\UDC\output\upp”
  
  ## Required stuff
  $Now = Get-Date
  $Days = “2”
  $TwoDays = $Now.AddDays(-$Days)
  
  ## Window Message
  "Checking for files newer than $Days days in:"
  "$TargetFolder1"
  "$TargetFolder2"
  ""
  "This may take 5-15 minutes . . ."
  
  ## Reset variable $OK to "0"
  $OK = 0
  
  get-childitem $TargetFolder1,$TargetFolder2 -filter *.xml |where-object {
      $FileLastWrite = $_.LastWriteTime
      if ($FileLastWrite -le $TwoDays) {
          $OK = $OK + "0"
      } else {
          $OK = $OK + "1"
      }
  }
  if ($OK -gt "0") {
      if ($Debug -eq "2") {
          ""
          "Found $OK file(s) newer than $Days days"
          }
      Add-Content $log "OK"
      if ($Debug -ge "1") {
          Add-Content $log "Found $OK file(s) newer than $Days days"
          }
      }
      else {
          if ($Debug -eq "2") {
          ""
          "Found $OK file(s) newer than $Days days"
          }
      Add-Content $log "Error"
      if ($Debug -ge "1") {
          Add-Content $log "Found $OK file(s) newer than $Days days"
          }
      }
  $Host.UI.RawUI.WindowTitle = $titdef
  </code></pre>