Reputation: 3162
This is what I am trying to do with powershell for zipping files.
I have Powershell_Community_Extensions installed so I use Write-zip
to do the job.
$Source = "\\network\share"
$files = Get-ChildItem -Path $Source | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-62)}
$files | Write-Zip -OutputPath $Source\Archive.zip -EntryPathRoot $Source -Append -Quiet
Remove-Item $files -Force
Issues:
-EntryPathRoot
with Write-zip
, otherwise it wont pick up network share"Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist."
, why it deleting file from C:\Windows\system32\
instead of \\network\share
Write-zip -append
did add files into the zip file, but it did not just add files in the root of that zip file, it actually created entire folder structure in the root of the zip and added newer filtered files in the end of that folder structure. I just want to add the newer filtered files into root of that zip file.Any idea please?
Upvotes: 0
Views: 248
Reputation: 19644
Utilizing the v5 *Archive cmdlets:
$Source = '\\network\share'
$Files = Get-ChildItem -Path $Source |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-62) }
Compress-Archive -Path $Files.FullName -DestinationPath $Source\Archive.zip -CompressionLevel Optimal -Update
$Files | Remove-Item -Force
Upvotes: 1