Saturday, October 4, 2014

Using PowerShell to zip and unzip package on Windows 10 and Windows Server Technical Preview (vNext)

There are build-in cmdlets in the PowerShell 5.0 of Windows 10 and Windows Server Technical Preview (vNext) to compress and extract zip files. Using these cmdlets, it's easy for us to write script files with compress and extract zip files function. "Compress-Archive" and "Expand-Archive" are new cmdlets to compress and extract zip files on PowerShell. These 2 cmdlets are under "Microsoft.PowerShell.Archive" module.


Now, the "Get-Command" displayed the version of PowerShell cmdlets.
First, I will perform "Expand-Archive" to extract zip files.
On a PowerShell console, perform "Expand-Archive -Path <The zip file location> -DestinationPath <Destination path for extraction> -Verbose".

Make sure there is a folder on destination path. if not, you get the following error message.


Then, I will perform "Compress-Archive" to create a zip package.

On the PowerShell console, perform "Compress-Archive -Path <Files or Directory path> -DestinationPath <Destination Path\"name".zip> -Verbose".


Remark: "Path" parameter supports more than 1 value.

With "Update" parameter, we can add a new file into an existing zip package.


By default, the compression level of "Compress-Archive" is "Optimal". We can also insert "CompressionLevel" to change it. There are 3 CompressionLevel" which are "Optimal",  "Fastest" and "NoCompression". For more information about "CompressionLevel", please read CompressionLevel Enumeration.


To zip a folder, perform "Compress-Archive -Path <Files or Directory path> -DestinationPath <Destination Path\"name".zip> -Verbose".


Remark: "Compress-Archive" and "Expand-Archive" only support zip package.

This posting is provided “AS IS” with no warranties, and confers no rights!

No comments:

Post a Comment