使用PowerShell的zip文件

时间:2017-12-06 17:49:21

标签: powershell zip

我正在尝试使用PowerShell来压缩文件。

  1. 排序超过xxx天的文件
  2. 将这些文件添加到zip文件中。
  3. 删除源文件。
  4. 我安装了Powershell_Community_Extensions,因此我使用Write-zip来完成这项工作。

    $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
    

    的问题:

    1. 我必须将-EntryPathRootWrite-zip一起使用,否则它将无法获取网络共享
    2. 删除项目也不会从网络共享中提取文件,它说"Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist.",为什么从C:\Windows\system32\而不是\\network\share删除文件
    3. Write-zip -append确实将文件添加到zip文件中,但它不只是在该zip文件的根目录中添加文件,它实际上在zip的根目录中创建了整个文件夹结构,并添加了更新的过滤文件。该文件夹结构的结尾。我只想将较新的过滤文件添加到该zip文件的根目录中。
    4. 请问好吗?

1 个答案:

答案 0 :(得分:1)

使用v5 * Archive cmdlet:

$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