PowerShell – Cleaning up old files with Compress-Archive & organizing by date

Windows Management Framework 5.0 introduces a few new Commandlets, notibly Compress-Archive which makes creating a .ZIP from within PowerShell extremely easy.  Below is a small project I worked on this week to clean up an FTP server used by automated processes.

This script grabs all files older than 24 hours, moves them into a folder by day, zips that folder, then deletes the folder.  Included error handling will capture the error and send via SMTP.

Any reccomendations/comments welcomed!

 

2 thoughts on “PowerShell – Cleaning up old files with Compress-Archive & organizing by date

  1. Hello great information… How would you change it from day to month. When I run it it creates a zip file for every day, would like a single zip per month.

    1. Muhammad, sorry for the late reply!
      Try changing $Directory = $path + $file.LastWriteTime.Date.ToString('yyyyMMdd') to ToString('yyyyMM'). This should work.

Leave a Reply

Your email address will not be published. Required fields are marked *