How to Zip a File or Folder using PowerShell?

PowerShell to zip a file or folder


Whether it’s sharing files with colleagues or backing up our own data, compressing files and folders into a zip archive can save storage space and reduce transfer times. Are you tired of manually zipping files or folders on your computer? Do you find it time-consuming and tedious to compress large amounts of data? Look no further than PowerShell! In this beginner’s guide, we will explore how to zip a file or folder using PowerShell, a powerful scripting language designed to automate tasks and manage configurations. Our step-by-step tutorial will cover various techniques, including how to create a zip file from a folder, add files to an existing zip archive, and zip multiple files and folders using PowerShell commands. Let’s get started!

Understanding the benefits of zipping files and folders

Zipping files and folders can be a great way to save disk space on your computer or to make it easier to share files with others. When you compress a file or folder, you’re essentially creating a smaller, more compact version of it that takes up less space on your hard drive. This can be especially useful if you have a lot of large files or folders that you don’t need to access very often.

Another benefit of zipping files and folders is that it can make it easier to share them with others. If you’re sending a file or folder to someone via email or another messaging platform, you might run into size limitations that prevent you from sending the file or folder as is. By compressing it, you can create a smaller version that’s easier to send, and the messaging platform won’t reject that. With PowerShell, you can automate this task and make it even more efficient.

But why should you use PowerShell instead of a graphical user interface (GUI) tool? First, using PowerShell can be much faster than using a GUI tool, especially if you need to compress large files or folders. You can create a compressed archive in seconds with just a few lines of code. Secondly, PowerShell allows for greater flexibility and automation when it comes to zipping files or folders. You can easily create scripts that automate the compression process for repetitive tasks.

How to zip a File using the Compress-Archive cmdlet?

Zipping a file with PowerShell is a quick and easy process with the Compress-Archive cmdlet. It was first introduced in PowerShell 5.0 and has been a popular tool ever since. To zip a single file using PowerShell, enter the following command:

Compress-Archive "C:\Temp\Logs.txt" "C:\Temp\"

The Compress-Archive cmdlet takes two parameters: the source file(s) and the destination file. The above command will create a Zip file named “” in the “C:\Temp” folder by compressing the “Logs.txt” file. Replace the parameters with your file names with the name of your source and zipped archive.

powershell zip file

That’s it! You have now zipped a single file using PowerShell. It’s a simple process that can save you a lot of time and effort.

Creating a Zip File from a Folder with PowerShell

Zipping an entire folder using PowerShell is a simple and effective method that can help you compress large files and folders into a single, more manageable file. The Compress-Archive cmdlet provides a simple, yet effective way to create compressed archives in PowerShell. To create a zip file from a folder using the native PowerShell command, use the following command, replacing “C:\Source” with the folder’s path and “C:\Destination\” with the desired output zip file path:

Compress-Archive -Path C:\Source -DestinationPath C:\Destination\

The cmdlet takes two mandatory parameters, the source path, and the destination path, to create a compressed archive. In the above example, we are compressing all the files and folders in the C:\Source directory and creating a compressed archive named in the C:\Destination directory.

Zipping Multiple Files and Folders using PowerShell

If you have multiple files or folders that you want to compress, you can use the Compress-Archive cmdlet to compress them all at once. Here is an example of how to compress multiple files or folders with PowerShell. In this example, we are compressing Invoice_v1.docx, and Invoice_v2.docx, located in the C:\Source directory and creating a compressed archive named in the C:\Destination directory.

Compress-Archive -Path "C:\Source\Invoice_v1.docx", "C:\Source\Invoice_v2.docx" -DestinationPath "C:\Destination\"

Once the command has finished running, you can check the compressed archive to make sure it was created correctly. You can do this by navigating to the location where the archive was saved and double-clicking on it to open it. You can also use the wildcard characters (asterisk) in the Path parameter, which allows you to select multiple files matching a pattern (But the “Compress-Archive -LiteralPath” doesn’t accept wildcards!). For example, let’s compress all .LOG files in a folder:

Compress-Archive -Path "C:\Logs\*.log" -DestinationPath "C:\Archive\"

You can also combine files and folders together in a zip file, as:

Compress-Archive -Path "C:\Source\Invoice_v1.docx", "C:\Reports" -DestinationPath "C:\Destination\"

Zip Each Sub-Folder in a Folder to create multiple archive files

How about archiving each sub-folder (at the first level of the given folder)?

# Parameters
$Source = "C:\Temp"
$Destination = "C:\Archive"
# Get all 1st level Sub-Folders in the Source
Get-ChildItem $Source -Directory | ForEach-Object {
    $FolderName = $_.Name
    $FolderPath = $_.FullName
    #Zip the Sub-Folder
    Compress-Archive -Path $FolderPath -DestinationPath $Destination\$FolderName 

This script compresses all subdirectories from a given root directory (Except empty sub-folders) into a separate zip file. Similarly, you can zip each file in a folder with the “-File” switch in Get-ChildItem cmdlet. Here is the source folder:

PowerShell Compress Each Sub-Folder

And the destination folder with zip files;

PowerShell Compress-Archive

Updating a zip archive by Adding New Files to It

If you want to add more folders or files to the zip archive, you can do it by using the -Update parameter followed by the path to the folder or file you want to add. For example:

Compress-Archive -Path [path to folder or file] -DestinationPath [Existing zip archive name].zip -Update

E.g., The below command will add “C:\Temp\NewLogs.txt” to the existing zip file “C:\Temp\”

Compress-Archive -Path "C:\Temp\NewLogs.txt" -DestinationPath "C:\Temp\" -Update

Make sure to replace ‘[path to folder or file]’ with the actual path to the folder or file you want to compress. If the destination file already exists, you can use the -Force parameter to overwrite it.

Customizing the compression level

If you want to customize the compression level for your zip archive, you can do it by selecting the compression levels available in PowerShell. PowerShell offers several compression levels, ranging from:

  • NoCompression (which doesn’t compress the files at all)
  • Optimal (which compresses the files as much as possible)
  • Fastest (Compressionlevel Fastest may reduce processing time. But it can result in a larger file size!)

Choosing the right compression level depends on your specific needs and the files you’re compressing. To adjust the compression level in PowerShell, you can use the ‘-CompressionLevel’ parameter followed by the desired level. For example, to set the compression level to ‘Optimal’, you would do this:

Compress-Archive -Path "C:\Temp\Docs" -DestinationPath "C:\Temp\" -CompressionLevel Optimal

You can replace ‘Optimal’ with the desired compression level and apply the fastest compression method if needed. When the -CompressionLevel parameter is not specified, the default setting is optimal.

Troubleshooting common issues with Compress-Archive

While the Compress-Archive cmdlet is a powerful tool for file compression, you may encounter some issues while using it. Here are some common issues and their solutions.

  • “Compress-Archive is not recognized as the name of a cmdlet” error message: This error occurs when the Compress-Archive cmdlet is not available in your PowerShell version. Microsoft has added this cmdlet in Windows PowerShell version 5.0. So, ensure your PowerShell version is up-to-date.
  • “The path is not of a legal form” error message: This error occurs when the source or destination path is incorrect or contains illegal characters. Ensure that you have specified the correct path and that it does not contain illegal characters.
  • “Access to the path is denied” error message: This error occurs when you do not have permission to access the source or destination path. Ensure that you have the necessary permissions to access the specified path.

Best practices for using Compress-Archive in PowerShell

Here are some best practices to keep in mind while using the Compress-Archive cmdlet in PowerShell.

  • Always specify the source and destination paths correctly to avoid errors.
  • Use meaningful file names for compressed archives to make them easier to identify. This can be especially useful if you have a lot of compressed files or folders that you need to keep track of.
  • Use compression levels to balance file size and compression time.
  • Test your PowerShell scripts thoroughly before using them in production environments.
  • Compress files and folders that you don’t need to access very often. If you’re constantly opening and closing a file or folder, it might not be worth the time and effort it takes to compress and extract.


Zipping files and folders using PowerShell is a quick and easy way to compress large files into smaller ones. In this beginner’s guide, we have covered the basics of creating a compressed archive, compressing multiple files or folders, and updating existing compressed archives. We have also discussed compression levels, troubleshooting common issues, and best practices for using Compress-Archive in PowerShell.

With this knowledge, Now that you have learned how to use Compress-Archive in PowerShell, you can start automating file compression tasks and managing files more efficiently. By following the steps outlined in this article, you can zip files and folders with ease.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

One thought on “How to Zip a File or Folder using PowerShell?

  • Superb blog article. Many thanks for taking the effort to detail and explain so well. Kudos!


Leave a Reply

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