Wednesday, November 18, 2015

Native zip commands in PowerShell 5

PowerShell 5 includes native commands for working with zip files. Woohoo!

They haven’t given us full functionality yet, but it’s definitely better than nothing.

We can create a zip file from a selection of files or folders using Compress-Archive.

001
Get-Item D:\test | Compress-Archive -DestinationPath D:\test2\TestArchive.zip

We can overwrite it with a new .zip file by using the –Force switch.

We can add or modify files in an existing .zip file using the –Update switch.

001
Get-Item D:\TCPSU | Compress-Archive -DestinationPath D:\test2\TestArchive.zip -Update

We unzip the .zip file using Expand-Archive.

001
Expand-Archive -Path D:\test2\TestArchive.zip -DestinationPath D:\test4

Use the –Force switch to overwrite files.

You can set the –CompressionLevel to Fastest instead of the default Optimal. Theoretically this will compress faster, but result in larger files. In my testing, there was no significant difference in either stat. Your mileage may vary depending on your specific files.

You can also set the –CompressionLevel to NoCompression. This can be useful if most of your files are already compressed; for example, .jpg, .msi, or .xlsx files. This allows you to combine them into a single package without wasting time trying to compress things that aren’t compressible.

What’s missing in this version

You can’t look at a .zip file and know what’s in it without unzipping it.

You can’t extract less than everything in the zip file.

You can’t use any archive type other than .zip.

The drive to get this command set into this version of PowerShell was the ability to use it with DSC. As a result, the features they focused on were the ones that are used within DSC. They did not have time to finish some of the features that would be useful in other scenarios. For those use cases, you will still have to use .Net 4.5 objects (or third party tools links 7-zip if you don’t have .Net 4.5).

No comments:

Post a Comment