PowerShell by Example: Archive Cmdlets
Working with ZIP files using native cmdlets with this code example demonstrating compressing files and folders, updating existing archives, and expanding archives to destination directories.
Code
# Compress a single file
Compress-Archive -Path "C:\Logs\app.log" -DestinationPath "C:\Backups\logs.zip"
# Compress multiple files and folders
$files = @("C:\Data\*", "C:\Config\settings.xml")
Compress-Archive -Path $files -DestinationPath "C:\Backups\data.zip" -CompressionLevel Optimal
# Update an existing archive (add files)
Compress-Archive -Path "C:\NewData\report.pdf" -DestinationPath "C:\Backups\data.zip" -Update
# Expand (Unzip) an archive
Expand-Archive -Path "C:\Backups\data.zip" -DestinationPath "C:\RestoredData" -Force
# Inspect archive contents (using .NET class)
$zipPath = "C:\Backups\data.zip"
[System.IO.Compression.ZipFile]::OpenRead($zipPath).Entries | Select-Object FullName, LengthExplanation
The Microsoft.PowerShell.Archive module provides native cmdlets for handling ZIP archives, eliminating the need for external tools or complex .NET class instantiation for basic compression tasks. The Compress-Archive cmdlet creates new ZIP files from specified paths, which can include individual files, directories, or a collection of both. It supports wildcard characters for bulk selection and offers a -CompressionLevel parameter (Optimal, Fastest, or NoCompression) to balance between file size reduction and processing speed.
Managing existing archives is streamlined through the -Update parameter of Compress-Archive, which allows users to append new files to an existing ZIP file without extracting and re-compressing the entire contents. Conversely, the Expand-Archive cmdlet extracts the contents of a ZIP file to a specified destination directory. The -Force parameter is crucial here, as it permits overwriting existing files in the destination folder, which would otherwise trigger an error.
While these cmdlets cover standard use cases, they are limited to the ZIP format and have constraints on file sizes (historically 2GB limits in older .NET framework versions). For advanced scenarios, such as inspecting archive contents without extraction or handling other formats, direct usage of the [System.IO.Compression.ZipFile] .NET class or third-party modules like 7Zip4PowerShell may be required. However, for everyday automation scripts involving log rotation, backup packaging, or artifact distribution, the native archive cmdlets provide a convenient and ubiquitous solution.
- Compress files and folders easily with
Compress-Archive - Update existing archives without full re-compression
- Extract contents safely using
Expand-Archive - Use .NET classes for advanced archive inspection
Code Breakdown
Compress-Archive creates standard .zip files from specified paths.-CompressionLevel Optimal balances size reduction and speed.-Update parameter appends files to an existing archive.Expand-Archive extracts zip contents to destination folder.
