title: Powershell: Log my deleted files
body: <p>New to powershell and need some help. My script searches the TEST folder and subfolders, then deletes files older than 21 days. I want to log all the deleted files in a log file.</p>
  
  <pre><code>cls 
  $Now = Get-Date 
  $Days = "21" 
  $Targetfolder = "c:\TEST" 
  #----- define LastWriteTime parameter based on $Days ---# 
  $LastWrite = $Now.AddDays(-$Days) 
  #----- get files based on lastwrite filter and specified folder ---# 
  $Files = Get-Childitem $TargetFolder -Include $Extension -Exclude *.xml -Recurse | where { $_.LastWriteTime -le "$LastWrite" } 
  foreach ($File in $Files) { 
      if ($File -ne $Null) { 
          write-host "Deleting File --&gt; $File" -ForegroundColor "Green" 
          Remove-Item $File.FullName | out-null 
      }
  }
  </code></pre>