CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Powershell – How to zip / compress / archive selected files or directories?

Windows PowerShell provides commands to zip (compress) and unzip the files. Through this article, we are going to discuss the PowerShell command to zip the selected files or directories.

Compress-Archive cmdlet – Compress / Zip / Archive the selected files or directories

PowerShell provides Compress-Archive cmdlet, to archive the specified files or directories. We can specify the list of files or directories to be archived through its -Path argument. If multiple files or directories are specified, we need to separate them using a comma (,) symbol. We can specify the absolute or relative paths to the files or directories through this parameter.

And always it is a good idea to keep the file or directory paths in double quotes when you pass them through the commands’ arguments.

Where to store the archived files or directories? We can specify the location where to store the zipped files or directories through the command’s -DestinationPath parameter. This parameter doesn’t allow multiple paths. If we pass multiple paths through this parameter, this command will throw the below error.

    + CategoryInfo          : InvalidData: (:) [Compress-Archive], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Compress-Archive

We can use absolute or relative paths in this parameter to specify where to store the archived files. Below is an example, to archive the specified file.

PS C:\PowerShell> Compress-Archive -Path ".\SampleFile.txt" -DestinationPath ".\SampleFile.zip"
PS C:\PowerShell> dir

    Directory: C:\PowerShell

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---l        10-24-2021     17:01       46109250 SampleFile.txt
-a---l        10-24-2021     17:05         112010 SampleFile.zip

We can also use wildcards in the -Path argument. When we use “*” wildcard, the Compress-Archive cmdlet archives all the files and sub-directories with in the specified directory. When we use “*.*”, it only archives the files within the specified directory, not in it’s sub-directories.

For example, below commands archives all the files in the specified directory, and archives the files in the specified directory & it’s sub-directories respectively.

PS C:\PowerShell> Compress-Archive -Path "C:\Photos\*.*" -DestinationPath ".\OnlyFewPhotos.zip"
PS C:\PowerShell> Compress-Archive -Path "C:\Movies\*" -DestinationPath "C:\Archive\AllMovies.zip"

Option to set Compression Level

Compress-Archive cmdlet provides flexibility to us to select the level of compression to apply on the selected files or folders/directories through its -CompressionLevel parameter. Through this parameter, we can pass the values Fastest, NoCompression, or Optimal. And we must enclose these values in double quotes (for example “Fastest“). By default, this command considers the compression level value, “Optimal” to compress the specified files or directories.

Usually, we select the “Fastest” compression level, to reduce the processing time. This command will use the fastest compression method available to compress the selected files or folders/directories and observe that you will not see a big difference in the file sizes. On the other hand, “Optimal” compression level will be used to compress the files or directories using available optimal compression methods which can result in smaller file sizes. And observe that, the processing time is more (depends on the file size) when we select the “Optimal” compression level.

PS C:\PowerShell> Compress-Archive -Path ".\SampleFile.txt" -DestinationPath ".\SampleFile-Fastest.zip" -CompressionLevel "Fastest"
PS C:\PowerShell> dir

    Directory: C:\PowerShell

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        10-24-2021     17:32         246052 SampleFile-Fastest.zip
-a---l        10-24-2021     17:01       46109250 SampleFile.txt
-a---l        10-24-2021     17:05         112010 SampleFile.zip

The compression level “NoCompression” is used to archive the files without compressing them.

PS C:\PowerShell> Compress-Archive -Path ".\SampleFile.txt" -DestinationPath ".\SampleFile-NoCompression.zip" -CompressionLevel "NoCompression"
PS C:\PowerShell> dir

    Directory: C:\PowerShell

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        10-24-2021     17:32         246052 SampleFile-Fastest.zip
-a----        10-24-2021     17:33       46116416 SampleFile-NoCompression.zip
-a---l        10-24-2021     17:01       46109250 SampleFile.txt
-a---l        10-24-2021     17:05         112010 SampleFile.zip

Observe the file sizes of each of these files, when using different compression levels.

Using pipe symbol to get inputs from other commands to archive the files or directories

We can combine Compress-Archive command with other command(s) in PowerShell. Commonly we use other commands to provide the input to this command to archives the files or directories. For example, by using below command we can archive all the “.png” image files which are returned from the Get-ChildItem cmdlet.

PS C:\PowerShell> Get-ChildItem -Path "C:\Images\*.png" | Compress-Archive -DestinationPath ".\png_images.zip"

Observe that there is no -Path parameter added in Compress-Archive cmdlet. It takes the inputs through the pipe from Get-ChildItem command.

Let’s close the article here, and we will discuss the PowerShell command to unzip the files, in our next article.

[..] David

Powershell – How to zip / compress / archive selected files or directories?

One thought on “Powershell – How to zip / compress / archive selected files or directories?

Leave a Reply

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

Scroll to top