Hi,
Please see if this works for you
$PDFDirectory ='\\usslcstor01\usslcnts'
$ArchiveLocation = '\\usslcstor01\usslcnts_archive'
$year = 2019
Get-ChildItem -Path $PDFDirectory | ForEach-Object{
Get-ChildItem -Path $_.FullName | ForEach-Object{
$destination = "$ArchiveLocation\$year\$($_.parent)\$($_.BaseName)"
if(-not (Test-Path -Path $destination)){ New-Item -Path $destination -ItemType Directory | Out-Null }
Get-ChildItem -Path $_.FullName -File *.pdf -Recurse | Where-Object {$_.LastWriteTime.Year -eq $year} | ForEach-Object{
Compress-Archive -Path $_.FullName -DestinationPath $destination\$year$(($_.LastWriteTime.Month).ToString('D2')).zip -CompressionLevel Optimal -Update
Remove-Item -Path $_.FullName
}
}
}
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.