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!

 

4 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.

  2. I often visit your website and have noticed that you don’t update it often. More frequent updates will give
    your page higher rank & authority in google. I know that writing articles takes a lot of time, but you
    can always help yourself with miftolo’s tools which will shorten the time of creating an article to a few
    seconds.

  3. I see you don’t monetize your page, don’t waste your traffic,
    you can earn additional bucks every month. You can use
    the best adsense alternative for any type of website (they approve all websites), for more info simply search
    in gooogle: boorfe’s tips monetize your website

Leave a Reply

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