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

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

Create the Zip file containing a directory

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

Create the ZIP file using a wildcard

1
2
3
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

1
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