0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Compress Files Quickly

Use Compress-Archive -Path 'files' -DestinationPath 'archive.zip' in PowerShell to compress files into a ZIP archive quickly.
📋

Examples

InputCompress-Archive -Path 'C:\Docs\file.txt' -DestinationPath 'C:\Docs\file.zip'
OutputCreates file.zip containing file.txt in C:\Docs
InputCompress-Archive -Path 'C:\Images\*' -DestinationPath 'C:\Backup\images.zip'
OutputCreates images.zip containing all files in C:\Images
InputCompress-Archive -Path 'C:\EmptyFolder\*' -DestinationPath 'C:\Backup\empty.zip'
OutputCreates empty.zip but it will be empty if no files exist
🧠

How to Think About It

To compress files in PowerShell, identify the files or folder path you want to archive, then use the built-in Compress-Archive cmdlet specifying the source and the destination ZIP file. This cmdlet handles creating the ZIP file and adding the files automatically.
📐

Algorithm

1
Get the path of files or folder to compress
2
Set the destination path for the ZIP archive
3
Use the Compress-Archive cmdlet with source and destination
4
Verify the ZIP file is created with the expected contents
💻

Code

powershell
Compress-Archive -Path 'C:\Example\*' -DestinationPath 'C:\Example\archive.zip'
Write-Output "Files compressed to archive.zip"
Output
Files compressed to archive.zip
🔍

Dry Run

Let's trace compressing all files in C:\Example to archive.zip

1

Identify files

Files in C:\Example: file1.txt, file2.txt

2

Run Compress-Archive

Compress-Archive -Path 'C:\Example\*' -DestinationPath 'C:\Example\archive.zip'

3

Output confirmation

Write-Output "Files compressed to archive.zip"

StepActionValue
1Files foundfile1.txt, file2.txt
2ZIP createdarchive.zip with file1.txt, file2.txt
3OutputFiles compressed to archive.zip
💡

Why This Works

Step 1: Using Compress-Archive

The Compress-Archive cmdlet is built into PowerShell and compresses files or folders into a ZIP file.

Step 2: Specifying paths

You provide the source files with -Path and the ZIP file location with -DestinationPath.

Step 3: Output confirmation

Using Write-Output prints a message confirming the compression completed.

🔄

Alternative Approaches

Using .NET System.IO.Compression
powershell
[System.IO.Compression.ZipFile]::CreateFromDirectory('C:\Example', 'C:\Example\archive.zip')
Write-Output "Compressed using .NET method"
This method uses .NET directly and can compress entire folders but requires PowerShell 5+ and the assembly loaded.
Using 7-Zip command line
powershell
Start-Process -FilePath '7z.exe' -ArgumentList 'a', 'C:\Example\archive.zip', 'C:\Example\*' -Wait
Write-Output "Compressed using 7-Zip"
Requires 7-Zip installed; offers more compression options but adds external dependency.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of files n being compressed because each file is read and added to the archive.

Space Complexity

The space needed is proportional to the total size of files compressed, as the ZIP archive stores all data.

Which Approach is Fastest?

Using Compress-Archive is fast and simple for most cases; .NET methods may be slightly faster for large folders, while 7-Zip offers better compression but requires extra setup.

ApproachTimeSpaceBest For
Compress-Archive cmdletO(n)O(n)Simple built-in compression
.NET ZipFile methodO(n)O(n)Compressing entire folders programmatically
7-Zip command lineO(n)O(n)Advanced compression with external tool
💡
Use wildcards like * in -Path to compress multiple files easily.
⚠️
Forgetting to include * when specifying a folder path causes no files to be compressed.