Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aev-mambro2/d31db3768b70a057b03dfae71739587b to your computer and use it in GitHub Desktop.
Save aev-mambro2/d31db3768b70a057b03dfae71739587b to your computer and use it in GitHub Desktop.
Powershell how to compress/zip the files in a folder based on age
# Get all log files older than 1 day in the logs folder.
$subjects = Get-ChildItem -Path "D:\logs" -Filter "*.log" -file | Where-Object {$_.LastWriteTime -le ((get-date).AddDays(-1))}
# Compress them into a zip file, overwriting ones with the same name.
$subjects | Compress-Archive -DestinationPath "A:\archives\logs.zip" -Force
# Remove the originals. Note that if compressing fails, items will not get removed.
$subjects | Remove-Item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment