Compress Old Files with PowerShell

 http://www.thesysadminhimself.com/2014/01/compress-old-files-with-powershell.html


The following PowerShell script will compress files that are older than the specified amount of time. It is handy for archiving IIS Logs, SQL Backups, etc.
The script uses 7-zip so you obviously have to have it installed (or the exe copied somewhere). It's using maximum compression which is resource intensive, if you don't want that just remove the "-mx9 -m0=lzma2" parameters.



The Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$path = "C:\inetpub\logs\LogFiles\"
$mask = "*.log"
 
$days = 7
 
$files = dir $path -Recurse -Include $mask | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)}
 
 
ForEach ($file in $files) {
 
    & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 ($file.FullName + ".7z") $file.FullName
    if ($LASTEXITCODE -eq 0) {
        Remove-Item $file
    }
 
}

Comments

Popular posts from this blog

Extracting and using a modified VMWare Player BIOS or UEFI firmware

Activate Synology Active Backup for Business for DSM 6

Windows 11 internet/account bypass