Netflex Blog

PowerShell script for archiving/compressing files


The following script archives files into zip files from a particular directory that are older than x days. Convenient to zip eg IIS logs. Of course, the script may also be used for other purposes.


How does the script work?

Change the path to the directories to be archived ($ IISlogDIR = “D: IIS_LOGDIR01”, “D: IIS_LOGDIR02”) and enter the number of days before the file is archived ($ZipOlderThanDays = “-7”).


The script checks if PowerShell v3 and .NET 4.5 are installed, if this is not the case an error message appears. The script will check for files to be archived, if there are none the script will end. If there are files to be archived (older than x days) a temporary folder will be created and the files will be moved into this folder. Then, the files in the temporary directory will be archived to the file backup_ <date> _ <time> .zip and the temporary directory will be removed. A summary of the archiving will be displayed on screen. Files with the extension .zip older than x days are left alone.



PowerShell script for archiving/compressing files

# Move-ToZip.ps1
# Moves files older than x days to a ZIP file
# Created: 04-09-2015
# Created by: Michiel Elderson (Netflex)
# Script Requires .NET 4.5 and Powershell 3+
# v1.1 (added support for multiple paths)

#Michiel Elderson: Define (IIS) log directory and files older than x days to ZIP.
# Modify these Variables

$ZipOlderThanDays = "-7"

#Michiel Elderson: Determine powershell version
if (($PSVersionTable.PSVersion).major -lt 3){
Write-host "Powershell Version not supported, update Powershell to v3+" -ForegroundColor Red

#Michiel Elderson: Determine .NET version
$DotNET = (Get-ItemProperty -Path 'HKLM:SoftwareMicrosoftNET Framework SetupNDPv4Full' -ErrorAction SilentlyContinue).Version -like '4.5*'
if ($DotNET -match "fase"){
Write-host ".Net version not supported, update to .Net v4.5" -ForegroundColor Red

#Michiel Elderson: Define some variables
$time = $(get-date -f HH_mm_ss)
$date = get-date -UFormat %Y.%m.%d

#Michiel Elderson: Run script foreach directory ($IISLOGDIR)
Foreach ($IISLogDir in $IISlogDIRS){
$backupfolder = "$IISlogDir" + "$date"
$source = $backupfolder
$destination = "$IISlogDir" + "Backup_" + "$date" + "_"+ "$time" + ".zip"

#Michiel Elderson: Check if directory exists
if (!(Test-Path $IISLogDir)){
Write-host "$IISLogDir DOES NOT EXIST" -ForegroundColor red

#Michiel Elderson: Check if there are files to ZIP, if not exit script
$FilestoMove = (get-childitem -Path $IISlogDir -Attributes !d | where-object {$_.lastwritetime -lt (get-date).addDays($ZipOlderThanDays) -and (!($ -like "*.zip"))})
$NumberOfFiles = ($FilestoMove).count
if (!($FilestoMove)){
Write-host "No files to ZIP in $IISLogDir" -ForegroundColor Red

#Michiel Elderson: Check if $backupfolder exists, if not create it.
if (!(Test-Path $backupfolder)){
new-item -Path $backupfolder -Type directory

#Michiel Elderson: Move files (older than x days) to $backupfolder
$FilestoMove | move-item -destination $backupfolder

#Michiel Elderson: Add ZIP assembly
Add-Type -assembly ""

#Michiel Elderson: Zip it!
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)

#Michiel Elderson: Delete backup directory (Exclude Zip files)
del $backupfolder -Recurse

#write some information to screen
Write-host "
Date: $date
Zipped $NumberOfFiles logfiles older than $ZipOlderThanDays days to $destination" -ForegroundColor yellow

Save the script to the file Move-ToZip.ps1 or download it here.
Change the variables $IISlogDIR (path to log directory) and $ZipOlderThanDays (archive files older than x)
The script requires Powershell v3 and .NET 4.5

Scroll to Top