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 “” 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).

compress-archive -path 'c:\wwwroot\logs' -destinationpath '.\' -compressionlevel optimal
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.

compress-archive -path “c:\wwwroot\logs\latest” -destinationpath “c:\wwwroot\logs\” -update -compressionlevel optimal

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

compress-archive test.txt

Unzip files with Expand-Archive

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

expand-archive -path 'c:\users\john\desktop\' -destinationpath '.\unzipped'
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.

function collect-iislog
	param ($zipfile,
	Compress-Archive -Path "c:\wwwroot\logs" -DestinationPath "c:\wwwroot\logs\$"
	Send-MailMessage -To $recipient -From "" -Subject "Log files from Company" -Body "Attached are the log files from the IIS application for your review" -SmtpServer -Attachments "c:\wwwroot\logs\$"	
collect-iislog -recipient -zipfile logs -sendmail


  1. ibrahim fazil 5 years ago

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

    can you tell me

    • @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:

      Install-Module -Name 7Zip4Powershell

      To unzip a file with a password:

      Expand-7Zip -ArchiveFileName ZippedFile.7z -Password "P@55word" -TargetPath C:\DestinationFolder

  2. cecilia 4 years ago

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

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

  3. Abhishek Sreedhar 3 years 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\

    Please help.

  4. Manoj 3 years ago

    Hey can i know how to untar .tar.gz/.tar via PowerShell only unzip extension is possible via PowerShell..

  5. Assuming a linux or mac platform, you'd run tar just like normal inside of powershell.  You don't need anything powershell specific.


    David F. 

Leave a reply

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


© 4sysops 2006 - 2023


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


Log in with your credentials


Forgot your details?

Create Account