Zipping files older than X days


Recently I was working with a firewall that wrote some tremendously large log files and was eating disk at an alarming rate, so to remedy this, and sharpen my Powershell pencil, I decided to write a script that could be setup with task scheduler to zip up older logfiles….


The script requires the Powershell Community Extensions (PSCX) module as it uses the Write-Zip cmdlet.


Running the script with no switches will prompt for the file type, path and age.  


To schedule this you would need to add a check for the PSCX module :

if(-not(get-module -name pscx -ea:silentlycontinue)){import-module pscx}


Then you can just invoke it with Zip-OldFiles.ps1 log c:\temp\logs 10

The script will ZIP every file older than 10 days in the C:\temp\logs folder, and delete the source file when done.   Resulting ZIP will be (eg. Firewall.log becomes



[Parameter(Position=0, Mandatory=$true)] 

[string]$filetypes = $(Read-host “Enter the type of files to ZIP (don’t include * or .)”),     

[Parameter(Position=1, Mandatory=$true)] 

[string]$TargetFolder = $(Read-host “Please Enter The Directory Path”),     

[Parameter(Position=2, Mandatory=$false)] 

[string]$Days = $(Read-Host “How Many Days?”)


if (Test-Path $TargetFolder)


  $Now = Get-Date

  $LastWrite = $Now.AddDays($days)

  $Files = get-childitem $TargetFolder -include *.$filetypes -recurse | Where {$_.LastWriteTime -le “$LastWrite”}

  if ($files | write-zip) {$files | remove-item}



  {Write-Host “The Folder $TargetFolder Does Not Exist!” -foregroundcolor “Red” -backgroundcolor “Black”}

Reader Comments

  1. Zip repair easily repair corrupt and damaged zip files CRC errors and fix zip files corruption issues

Comments are closed.