Since PowerShell 5.0 (built into Windows 10 and Server 2016), Microsoft included native compression/decompression cmdlets:
# Zip files
Compress-Archive -Path C:\source\* -DestinationPath C:\output\archive.zip -CompressionLevel Optimal
# Unzip files
Expand-Archive -Path C:\archives\archive.zip -DestinationPath C:\extracted\
For true one-liners that work in older PowerShell versions, you can use these approaches:
# Zip using COM object (works in PS 2.0+)
[System.IO.Compression.ZipFile]::CreateFromDirectory('C:\source', 'C:\output\archive.zip')
# Unzip using COM object
[System.IO.Compression.ZipFile]::ExtractToDirectory('C:\archives\archive.zip', 'C:\extracted')
For more control over the compression process:
# Zip with password protection (requires 7zip)
& "C:\Program Files\7-Zip\7z.exe" a -tzip -pYourPassword encrypted.zip C:\files\*
# Unzip specific files only
Expand-Archive -Path archive.zip -DestinationPath output -Force
(Get-ChildItem output).Where{$_.Name -notmatch '\.txt$'} | Remove-Item
When dealing with large archives, consider these optimizations:
# Split large zip files
Compress-Archive -Path .\largefiles\* -DestinationPath split.zip -CompressionLevel Fastest
# Use streams for better memory management
$zip = [System.IO.Compression.ZipFile]::Open('large.zip','Update')
$entry = $zip.CreateEntry('file.txt')
$stream = $entry.Open()
[System.IO.StreamWriter]::new($stream).WriteLine("Content")
$stream.Close()
$zip.Dispose()
Always implement proper error handling:
try {
Expand-Archive -Path corrupt.zip -DestinationPath output -ErrorAction Stop
}
catch [System.IO.InvalidDataException] {
Write-Warning "The ZIP file is corrupt: $_"
# Attempt repair with 7zip
& "C:\Program Files\7-Zip\7z.exe" x corrupt.zip -ooutput -y
}
Newer PowerShell versions offer additional features:
# Parallel compression (PowerShell 7.2+)
1..10 | ForEach-Object -Parallel {
Compress-Archive -Path "file$_.txt" -DestinationPath "archive$_.zip"
} -ThrottleLimit 5
# Cross-platform zip (Linux/macOS compatible)
if ($IsLinux) {
tar -czf archive.tar.gz ./files
} else {
Compress-Archive -Path ./files -DestinationPath archive.zip
}
Since PowerShell 5.0 (included with Windows 10+), you can use the built-in Compress-Archive
and Expand-Archive
cmdlets:
# Basic compression
Compress-Archive -Path "C:\Data\*" -DestinationPath "C:\Archives\output.zip"
# Force overwrite existing zip
Compress-Archive -Path "C:\Data\*.txt" -DestinationPath "C:\Archives\textfiles.zip" -Update -CompressionLevel Optimal
# Simple extraction
Expand-Archive -Path "C:\Archives\output.zip" -DestinationPath "C:\Extracted"
For more control or backward compatibility, try these single-line solutions:
# Using .NET 4.5+ (one-line zip)
[System.IO.Compression.ZipFile]::CreateFromDirectory("C:\Data", "C:\Archives\data.zip")
# One-line unzip
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\Archives\data.zip", "C:\Extracted")
For advanced scenarios, consider 7-Zip via PowerShell:
# Using 7-Zip module (install first: Install-Module -Name 7Zip4PowerShell)
Compress-7Zip -Path "C:\Data" -ArchiveFileName "data.7z" -Format Zip -CompressionLevel Ultra
Expand-7Zip -ArchiveFileName "data.7z" -TargetPath "C:\Extracted"
Native PowerShell doesn't support password protection, but you can use:
# Using DotNetZip (requires Add-Type -Path "Ionic.Zip.dll")
$zip = New-Object Ionic.Zip.ZipFile("secure.zip")
$zip.Password = "P@ssw0rd"
$zip.AddDirectory("C:\SensitiveData")
$zip.Save()
$zip.Dispose()
For large files, these optimizations help:
# Multi-threaded compression (PowerShell 7+)
$compress = @{
Path = "C:\BigData\*"
DestinationPath = "bigdata.zip"
CompressionLevel = "Fastest"
}
Compress-Archive @compress -AsJob