AntiMalware Status

Compress files or folders with PowerShell

The Compress-Archive cmdlet creates a compressed, or zipped, archive file from one or more specified files or directories. An archive packages multiple files, with optional compression, into a single zipped file for easier distribution and storage.

The Compress-Archive cmdlet uses the Microsoft .NET API System.IO.Compression.ZipArchive to compress files. The maximum file size is 2 GB because there’s a limitation of the underlying API.

Example usage:

Create the ZIP file containing a single file

Compress-Archive -Path "C:\test\test.txt" "C:\test.zip" -compressionlevel Optimal

Create the Zip file containing a directory

Compress-Archive -Path "C:\test" "C:\test.zip" -compressionlevel Optimal

Create the ZIP file using a wildcard

Compress-Archive -Path "C:\test*" "C:\test.zip" -compressionlevel Fastest
Compress-Archive -Path "C:\test1\*.txt" "C:\test.zip" -compressionlevel Optimal
Compress-Archive -Path "C:\test1\*.txt","C:\test2\*.txt" "C:\test.zip" 

Extract the content of the ZIP file

Expand-Archive -Path "C:\test.zip" -DestinationPath "C:\DESTINATION"

The compression level switch:

Specifies how much compression to apply when you are creating the archive file. Faster compression requires less time to create the file, but can result in larger file sizes. The acceptable values for this parameter are:

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.

If this parameter is not specified, the command uses the default value, Optimal.

This command is available in PowerShell v.5 + – more on usage of the cmdlet – Compress-Archive – PowerShell – SS64.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.