The cmdlet Compress-Archive and Expand-Archive have been introduced in PowerShell 5 and allow you to easily zip (compress) and unzip (extract) archives on the the command line. Using these cmdlets can save time and disk space when packaging up log files or folders for safe keeping.

Derek Schauland

Derek Schauland has been working in IT for more than 20 years. For almost as long, he has been authoring technical articles to help others learn about new technologies. Derek is a multiyear Microsoft MVP in Cloud and Datacenter Management.

Latest posts by Derek Schauland (see all)

If you only want to send a compressed log file to a vendor, it is probably faster to right-click the file in File Explorer and choose the Send To Compressed folder. Many third-party tools exist that offer additional features, such as 7-Zip, which comes with a command line version. However, the new cmdlets are very useful if you have to automate a task with PowerShell in which you have to compress or extract data. I will give you an example at the end of this post.

Microsoft has added command line–driven compression to PowerShell in the latest release through two cmdlets:

  • Compress-Archive – used to create compressed (zip) files
  • Expand-Archive – used to extract files from their zip file containers

Zip files with Compress-Archive ^

The following example compresses the IIS log folder in the archive file “logs.zip.” The CompressionLevel parameter supports the values of Fastest (fast, creates larger zip files), NoCompression (folder files are combined in a single file without compression), and Optimal (slow, creates smaller zip files).

Compressing files with PowerShell using the Compress-Archive cmdlet

Compressing files with PowerShell using the Compress-Archive cmdlet

Instead of the -Path parameter, you can also use -LiteralPath, which ensures that wildcard characters are not interpreted by PowerShell. The Compress-Archive cmdlet also has an -Update parameter that allows you to replace older file versions with new ones.

If you work on the command line, you can save yourself some typing by omitting the parameter names:

Unzip files with Expand-Archive

The following example unzips the archive iislogs.zip to a new folder in the current directory.

Expanding compressed files using PowerShell and the Expand-Archive cmdlet

Expanding compressed files using PowerShell and the Expand-Archive cmdlet

Note that if you omit the -DestinationPath parameter, PowerShell will automatically create a new folder using the name of the zip archive. In the example above, the new folder would then be "iislogs."

A usage example ^

Suppose John is a system administrator trying to work through a problem with a set of log files from an IIS application and needs to send the files to the application vendor for further research. Since the application is in production and is running on a server that is heavily used, trying to amass the log files while the system is busy is not something John wants to do. Instead, he is going to use PowerShell to script the entire process and have everything sent off to support at the scheduled time.

 

Win the monthly 4sysops member prize for IT pros

Share
1+

Related Posts

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account