Manage Learn to apply best practices and optimize your operations.

PowerShell Command Line Zip Handling

Today I learned something I should’ve known years ago. PowerShell command line zip handling is simple and easy, thanks to the built-in Compress-Archive and Expand-Archive cmdlets. Turns out these are extensively documented in the PowerShell 6 Scripting docs (see previous links). They are also easy to learn and use.

Using PowerShell Command Line Zip Handling

Mostly the two command use the same syntax and support the same parameters. And invariably those parameters are optional, so you can use ’em (or not) as your needs dictate. I’ll provide a couple of simple examples, to help show how things work.

Lifted from the afore-linked references, these diagrams show basic parameters for both commands.
[Click image for full-size view.]

For grins, I renamed a recent ZIP file to Test.zip and created a trio of simple text files named test<nn>.txt, where nn was 01, 02 and 03. These were all placed in a directory whose complete path specification was H:/TestPSZipStuff, as shown here:

I set up a bogus directory with a ZIP file and some text files to play around with.
[Click image for full-size view.]

Example: Compress txt files into a ZIP archive

Here’s a screen capture of the PowerShell syntax I used to Zip all of the text files into a Zipfile named Textfiles.zip (notice use of .\ for current directory, and the wildcard character):

This makes short work of grabbing all text (.txt) files up into a single ZIP file.
[Click image for full-size view.]

Notice that I also used the CompressionLevel parameter. This is explained in the afore-linked doc as telling the command how much compression to apply when creating the ZIP file. Acceptable values include (quoted verbatim from the cmdlet info):

  • Fastest. Use the fastest compression method available to decrease processing time; this can result in larger file sizes.
  • NoCompression. Do not compress the source files.
  • Optimal. Processing time is dependent on file size

Example: Expand ZIP file into subdirectory

Here’s a screencap that shows me expanding the contents of Test.zip into a subdirectory named TestZipFiles. I include a couple of dir commands to show a new directory inside the home directory TestPSZipStuff, and then to show the contents of that directory (another subdirectory inside which the executable and other files for ShadowExplorer reside):

Notice the use of the current directory as “./” and the default supply of .zip as the file extension.
[Click image for full-size view.]

I you play around with these cmdlets and learn the ins and outs of their parameters and syntax, it won’t take long to get comfortable with them. Good stuff!

[Note Added May 8, ’19; circa 4 PM Central] Microsoft Insider MVP Luigi Bruno reminded me, quite correctly, that these particular PowerShell cmdlets are available only in PowerShell versions 5 and higher. If you’re running a relatively current Windows 10, they’ll work for you with the built-in version. If you’re running an older Windows version (including 7 and 8.1) you’ll need to upgrade PowerShell to gain access to these cmdlets and their capabilities. Thanks Luigi!