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.
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.

 

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+
Share
5 Comments
  1. ibrahim fazil 1 year ago

    what is  command for unzip the file (with password) ?

    can you tell me

    0

    • @ibrahim

      The native Microsoft.PowerShell.Archive module does not handle passwords.

      However, you can use the 7Zip4Powershell module from the PowerShell gallery.

      To install it:

      To unzip a file with a password:

      1+

  2. cecilia 1 year ago

    HI, May I know if the compressionlevel can speed up the compression process?

    0

    • @cecilia

      The real answer is it depends.

      However, generally speaking:

      • the better the compression level, the slower the compression process
      • the faster the compression process, the lower the compression level

      And there is another parameter in the equation: some algorithms are more efficient  to compress specific type of files.

      Therefore, I advice you to test and measure the speed result.

      1+

  3. Abhishek Sreedhar 1 week ago

    Hi, Thanks for the useful information. It works. But in my case, the date in the file name changes everyday. How do I pass an argument/parameter to the powershell script with the file name. I am using the below command in my powershell script. 

    Set-ExecutionPolicy Unrestricted
    Compress-Archive -LiteralPath \\Bpn-rosebifi-02\Shares\Temp\Abhishek\Rege_OptIn_ESP_Score\RegE_OptIN_Report2_20200521.csv -DestinationPath \\Bpn-rosebifi-02\Shares\Temp\Abhishek\Rege_OptIn_ESP_Score\RegE_OptIN_Report2_20200521.zip
     

    Please help.

    0

Leave a reply

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

*

© 4sysops 2006 - 2020

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account