0

This is what I am trying to do with powershell for zipping files.

  1. Sort out files older than xxx days
  2. Add those files in a zip file.
  3. Delete the source 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:

  1. I have to use -EntryPathRoot with Write-zip, otherwise it wont pick up network share
  2. Remove-item also wont pickup files from network share, it says "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
  3. 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?

2
  • Which version of PowerShell are you on? If v5, there are built-in *Archive cmdlets Commented Dec 6, 2017 at 18:11
  • hum....didn't know that, I m on v5. I ll try it now. Commented Dec 6, 2017 at 18:50

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.