简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM