How to Zip and Unzip Files Using PowerShell – Guide

PowerShell added the functionality of the archiving module (Microsoft.PowerShell.Archive) in 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, but 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 your .Net Framework if you are below version 4.5 ..

If you want to send a compressed log file to a provider, it is 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 the final of this post. ..

Since the application is in production and running on a heavily loaded server, John does not 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

PS C:>zip -Path C:\Invoices -DestinationPath C:\ Archives The Invoices.zip file will be created in the C:\Archives folder.

Compress-Archive -Path C:Invoices -DestinationPath C:ArchivesInvoices

-LiteralPath “Invoices” -LiteralPath “Invoices.txt”

Compress-Archive -LiteralPath C:InvoicesFile1.txt, C:InvoicesFile2.txt -DestinationPath C:ArchivesInvoices -Force

PowerShell -Force -Update Invoices.zip To overwrite an existing file with a new file, use the -Overwrite parameter: PowerShell -Overwrite myfile.txt ..

Compress-Archive -Path C:Invoices* -Update -DestinationPath C:ArchivesInvoices -Force ..

Using PowerShell to Unzip Files

Expand-Archive -Path “C:\Invoices” -FileName “Invoices.zip” -DestinationFolder “C:\InvoicesUnzipped”

Archive -LiteralPath C:ArchivesInvoices.Zip -DestinationPath C: InvoicesUnzipped

Expand-Archive will automatically create the folder if needed, but if the files you want to unzip already exist in the destination folder, it will return an error. You can replace files in the destination folder by adding the -Force parameter to the command.

Final note

How to Zip and Unzip Files Using PowerShell is a guide that will show you how to zip and unzip files using PowerShell. This guide is designed to help you in case you have any questions about this article or if you want to share it with your friends.