Created
May 18, 2022 13:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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