Tech Blog

Coding, IT Administration

How to compress/zip files older than x days and delete the original files post compression using power shell script.

By Mahesha HC2 min read
← Back to blog

If your application is generating huge log files everyday or you have a big database backup which needs to be stored somewhere then definitely storage space is one of the big concerns here. By compressing the files you can save a lot of storage space. But compressing files manually on a daily basis is one tedious job!

Here’s a solution by power shell script which will compress the files older than x days and delete the original files post verifying the creation of compressed zip file.

Prerequisites:-

  • Install 7zip tool on your windows machine through this link – https://www.7-zip.org/download.html
    NOTE:- 7-Zip is a free and open-source file archiver for compressing and uncompressing files. You can also use Compress-Archive cmdlet, But the maximum file size is 2 GB because there’s a limitation of the underlying API.
  • Mention the number of days before which the files have to be compressed in the LastWrite variable.
  • Change the folder path of the files and Filter(type of the file) as per your need.
  • Save the below power shell script with .ps1 extension and run it. post script execution you should be able to see compressed files with the name of the original file and the date of compression.
  • You can schedule this script to run on a daily basis using Windows Task Scheduler.

Power Shell script:-

## Adding 7zip to environment variables.
$7zippath = “$env:ProgramFiles\7-Zip\7z.exe”
Set-Alias 7zip $7zippath

## Replace x with the number of days before which the files have to be compressed.
$LastWrite = (get-date).AddDays(-x)
$date=(Get-Date).ToString(‘yyyyMMdd’)

## Replace Path and Filter as per your need.
$Files = Get-ChildItem -Path “D:\Logs_folder” -Filter “*.txt” -Recurse -File | Where-Object {$_.LastWriteTime -le $LastWrite}

ForEach ($File in $Files) {
“Compressing Log $File”
7zip a “$($File.fullname)$date.zip” “D:\Logs_folder\$File”

if((Test-Path “$($File.fullname)$date.zip”))
{
“Log Compression succeeded, so deleting D:\Logs_folder\$File”
Remove-Item -Path “D:\Logs_folder\$File”
}
else
{
“Log Compression FAILED, Please check parameters are passed correctly”
}
}

Keep reading

You may be interested in

Browse all articles →
Using SCEP Certificates with Okta and SureMDM for Zero Trust Security
August 5, 20257 min read

Using SCEP Certificates with Okta and SureMDM for Zero Trust Security

For businesses embracing zero-trust principles, secure certificate-based authentication isn’t just nice to have—it’s a necessity. With Okta as your Identity Provider (IdP) and SureMDM as your unified endpoint management solution, you can now seamlessly distribute and manage device certificates using Simple Certificate Enrollment Protocol (SCEP). This blog will guide you through: Why Use SCEP with […]

By Sunny KumarRead More →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Setup minio storage in Ubuntu with Nginx
May 12, 20235 min read

Setup minio storage in Ubuntu with Nginx

Minio Storage : Minio is an open source object storage server that stores unstructured data such as photos, videos, log files, backups, and container/VM images. It is a lightweight, reliable, and secure alternative to hosting large amounts of data on distributed network drives or in the cloud. Minio is a cloud storage service that offers […]

By Malkeet SinghRead More →