- Zip and unzip with PowerShell - Fri, Jun 3 2016
- Auto login to Azure with PowerShell - Fri, Apr 29 2016
If you only want to send a compressed log file to a vendor, it is probably faster to right-click the file in File Explorer and choose the Send To Compressed folder. Many third-party tools exist that offer additional features, such as 7-Zip, which comes with a command line version. However, the new cmdlets are very useful if you have to automate a task with PowerShell in which you have to compress or extract data. I will give you an example at the end of this post.
Microsoft has added command line–driven compression to PowerShell in the latest release through two cmdlets:
- Compress-Archive – used to create compressed (zip) files
- Expand-Archive – used to extract files from their zip file containers
Zip files with Compress-Archive
The following example compresses the IIS log folder in the archive file “logs.zip.” The CompressionLevel parameter supports the values of Fastest (fast, creates larger zip files), NoCompression (folder files are combined in a single file without compression), and Optimal (slow, creates smaller zip files).
compress-archive -path 'c:\wwwroot\logs' -destinationpath '.\logs.zip' -compressionlevel optimal
Instead of the -Path parameter, you can also use -LiteralPath, which ensures that wildcard characters are not interpreted by PowerShell. The Compress-Archive cmdlet also has an -Update parameter that allows you to replace older file versions with new ones.
compress-archive -path “c:\wwwroot\logs\latest” -destinationpath “c:\wwwroot\logs\logs.zip” -update -compressionlevel optimal
If you work on the command line, you can save yourself some typing by omitting the parameter names:
compress-archive test.txt test.zip
Unzip files with Expand-Archive
The following example unzips the archive iislogs.zip to a new folder in the current directory.
expand-archive -path 'c:\users\john\desktop\iislogs.zip' -destinationpath '.\unzipped'
Note that if you omit the -DestinationPath parameter, PowerShell will automatically create a new folder using the name of the zip archive. In the example above, the new folder would then be "iislogs."
A usage example
Suppose John is a system administrator trying to work through a problem with a set of log files from an IIS application and needs to send the files to the application vendor for further research. Since the application is in production and is running on a server that is heavily used, trying to amass the log files while the system is busy is not something John wants to do. Instead, he is going to use PowerShell to script the entire process and have everything sent off to support at the scheduled time.
function collect-iislog { param ($zipfile, $recipient, [switch]$sendmail) Compress-Archive -Path "c:\wwwroot\logs" -DestinationPath "c:\wwwroot\logs\$sipfile.zip" Send-MailMessage -To $recipient -From "support@company.com" -Subject "Log files from Company" -Body "Attached are the log files from the IIS application for your review" -SmtpServer mail.company.com -Attachments "c:\wwwroot\logs\$zipfile.zip" } collect-iislog -recipient support@vendor.com -zipfile logs -sendmail
what is command for unzip the file (with password) ?
can you tell me
@ibrahim
The native Microsoft.PowerShell.Archive module does not handle passwords.
However, you can use the 7Zip4Powershell module from the PowerShell gallery.
To install it:
To unzip a file with a password:
HI, May I know if the compressionlevel can speed up the compression process?
@cecilia
The real answer is it depends.
However, generally speaking:
And there is another parameter in the equation: some algorithms are more efficient to compress specific type of files.
Therefore, I advice you to test and measure the speed result.
Hi, Thanks for the useful information. It works. But in my case, the date in the file name changes everyday. How do I pass an argument/parameter to the powershell script with the file name. I am using the below command in my powershell script.
Set-ExecutionPolicy Unrestricted
Compress-Archive -LiteralPath \\Bpn-rosebifi-02\Shares\Temp\Abhishek\Rege_OptIn_ESP_Score\RegE_OptIN_Report2_20200521.csv -DestinationPath \\Bpn-rosebifi-02\Shares\Temp\Abhishek\Rege_OptIn_ESP_Score\RegE_OptIN_Report2_20200521.zip
Please help.
Hey can i know how to untar .tar.gz/.tar via PowerShell only unzip extension is possible via PowerShell..
Assuming a linux or mac platform, you'd run tar just like normal inside of powershell. You don't need anything powershell specific.
David F.