How to Zip and Unzip Files Using PowerShell

How to Zip and Unzip Files Using PowerShell

This guide is about How to Zip and Unzip Files Using PowerShell. So read this free guide, How to Zip and Unzip Files Using PowerShell. If you have query related to same article you may contact us.

How to Zip and Unzip Files Using PowerShell – Guide

It is easy to zip or extract (unzip) files or folders with PowerShell. PowerShell added the functionality of the archiving module (Microsoft.PowerShell.Archive) of PowerShell version 5.1. If you have an older version of PowerShell (version 4.0 or lower), you can download and install the module from the website or from the command line. However, manually downloading and installing or copying the module file to the modules folder does not work because it requires some dependent DLL files provided by .Net Framework 4.5. Therefore, you should also consider upgrading the .Net Framework if you are below version 4.5.

If you just want to send a compressed log file to a provider, it’s probably faster to right-click the file in File Explorer and select the “Send to Compressed” folder. There are many third-party tools that provide additional functionality. However, the new cmdlets are very useful when you need to use PowerShell to automate a task where you need to compress or extract data. I will give an example in final of this post.

Since the application is in production and running on a heavily loaded server, John doesn’t want to try to collect the log files while the system is busy. Instead, it uses PowerShell to script the entire process and send it all to support at the scheduled time.

Using PowerShell to Create Zip Files

Let’s start by using PowerShell to compress files into a new zip file. All you need to do is use the -Path parameter to specify the folder you want to zip and the –DestinationPath parameter to specify the name of the file you want to create. The command below will zip the Invoices folder in the C root directory and create a file called Invoices.zip in the Archives folder:

  • Compress-Archive -Path C:Invoices -DestinationPath C:ArchivesInvoices
  • Alternatively, we can zip the files in the Invoices folder individually using -LiteralPath instead of –Path. This command creates a file with just the two files explicitly listed in the –LiteralPath parameter:

  • Compress-Archive -LiteralPath C:InvoicesFile1.txt, C:InvoicesFile2.txt -DestinationPath C:ArchivesInvoices -Force
  • Note that I added the -Force parameter to replace the file I created using the first command. Without the -Force parameter, you cannot overwrite existing files and PowerShell will prompt you to add files to the file. To add files to an archive, use the -Update parameter. The command below adds all the files from the Invoices folder to my existing Invoices.zip file:

  • Compress-Archive -Path C:Invoices* -Update -DestinationPath C:ArchivesInvoices
  • Using PowerShell to Unzip Files

    Extracting files from an archive is even easier than creating one. All you need to do is specify the filename and destination folder for the unzipped files. The command below extracts the contents of the Invoices.zip file to a folder named InvoicesUnzipped using the Expand-Archive cmdlet.

  • Expand-Archive -LiteralPath C:ArchivesInvoices.Zip -DestinationPath C: InvoicesUnzipped
  • The folder where you want to unzip the files does not have to exist; Expand-Archive will automatically create the folder if needed. But if the files you want to unzip already exist in the destination folder, Expand-Archive will return an error. You can replace files in the destination folder by adding the -Force parameter to the command.

    Final note

    I hope you like the guide How to Zip and Unzip Files Using PowerShell. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

    We will be happy to hear your thoughts

    Leave a reply

    How to Guides, Best Lists, Top 10 and Tech News - Compsmag
    Logo