0

I'm pretty new to Powershell in general, I've come across some partial solutions, but I can't find the right fit. Ideally I would like to Archive (Zip) with Powershell but only files of a certain age (x days/months), but I would prefer keeping the folder structure and also add exclusions. Here's what I've been trying to use (without success):

$inputFolder = "C:\Temp\Test"
$excludeFolders = @("\subfolderToKeep")
$ouputFileName="C:\Temp\archive.zip"
$Daysback = "-5"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)

$tempFolder = [System.IO.Path]::GetTempFileName()
Remove-Item $tempFolder -Force
New-Item -Type  Directory -Path $tempFolder -Force
$exclude =@()
$excludeFolders | ForEach-Object {
 $exclude+=(Join-Path $inputFolder $_) 
 Get-ChildItem (Join-Path $inputFolder $_) -Recurse | 
  ForEach-Object{$exclude+=$_.FullName}}
Get-ChildItem $inputFolder -Recurse | Where-Object { $_.FullName -notin $exclude -and $_.LastWriteTime -lt $DatetoDelete } |
 Copy-Item -Destination {Join-Path $tempFolder $_.FullName.Substring($inputFolder.length)}

Get-ChildItem $tempFolder |
Compress-Archive -DestinationPath $ouputFileName -Update

The current setup would work without the date filtering:

-and $_.LastWriteTime -lt $DatetoDelete

G Tuli
  • 3
  • 7

0 Answers0