title: Powershell log deleted files
body: <p><strong>The script searches all folders and subfolders and delete the oldest file when the number of files is>5. Everything works fine, but I want also log all the delete Files as a record in a log-file.</strong></p>
  
  <p>How can I log the deleted files ? </p>
  
  <p>Here the Script.</p>
  
  <pre><code>$path = "C:\test\1"
  $keep = 3
  $strLogFileName   = "c:\test\yourlogfile.log";
  
  $dirs = Get-ChildItem -Path $path -Recurse | Where-Object {$_.PsIsContainer}
  foreach ($dir in $dirs) {
      $files = Get-ChildItem -Path $dir.FullName | Where-Object {-not $_.PsIsContainer -and $_.name -like "*.zip"}
      if ($files.Count -gt $keep) {
          $files | Sort-Object CreationTime -desc| Select-Object -First ($files.Count - $keep) | Remove-Item -Force 
      ***{write-host “Deleting File $File” -foregroundcolor “Red”; Remove-Item $File | out-null}*** 
      }
  }
  </code></pre>