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.
Compress-Archive -Path "c:\wwwroot\logs" -DestinationPath "c:\wwwroot\logs\$sipfile.zip"
Send-MailMessage -To $recipient -From "firstname.lastname@example.org" -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 email@example.com -zipfile logs -sendmail