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:\" -compressionlevel Optimal

Create the Zip file containing a directory

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

Create the ZIP file using a wildcard

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

Extract the content of the ZIP file

Expand-Archive -Path "C:\" -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 –

Leave a Reply