Root Loop
Root Loop

Reputation: 3162

zip files using powershell

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?

Upvotes: 0

Views: 248

Answers (1)

Maximilian Burszley
Maximilian Burszley

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

Related Questions