I've written code to zip files older than 7 days from a source folder to a subfolder and then delete the original files. My code works best with Compress-Archive
and Remove-Item
cmdlets with fewer files, but takes more time and system memory for a large volume of files.
So, I'm working on a solution using 7zip instead as it's faster.
Below code does zipping correctly but not limit itself to files older than 7 days and deletes all the files from source folder. It should zip and delete only files older than 7 days.
Is there anything wrong with the code.
if (-not (test-path "$env:ProgramFiles7-Zip7z.exe")) {throw "$env:ProgramFiles7-Zip7z.exe needed"}
set-alias 7z "$env:ProgramFiles7-Zip7z.exe"
$Days = "7"
$Date = Get-Date -format yyyy-MM-dd_HH-mm
$limit = (Get-Date).AddDays(-$Days)
$filePath = "C:Users529817New folder1New folder_2"
Where LastWriteTime -lt $limit | 7z a -t7z -sdel "C:Users529817New folder1New folder_2ARCHIVE$Date.7z" "$filePath"
question from:https://stackoverflow.com/questions/65836287/zipping-using-powershell