Zip and Unzip File in Powershell 4

后端 未结 5 1670
一生所求
一生所求 2021-02-13 22:14

I am using Windows Server 2012 R2 (64 bit). I have powershell version 4 available in it. I am trying to zip and unzip files. When I try Write-Zip command, it throws me following

5条回答
  •  梦毁少年i
    2021-02-13 22:31

    Write-Zip seems to be part of http://pscx.codeplex.com/ that require a separate installation before you can use it.

    However, if you just want to create a Zip archive from a folder, you could just run

    $source = "c:\temp\source"
    $archive = "c:\temp\archive.zip"
    
    Add-Type -assembly "system.io.compression.filesystem"
    [io.compression.zipfile]::CreateFromDirectory($source, $archive)
    

    This utilizes the CreateFromDirectory method from the .NET Framework class ZipFile. It creates a zip archive from the files located inside the $source folder and creates an archive as defined in the $archive variable. Note, ZipFile class was introduced in .NET Framework 4.5

提交回复
热议问题