Have you ever wanted to compress all the subfolders in a specific folder to individual Zip files?
There are two Powershell scripts below. To use them, create a shortcut to the script using the following command in the shortcut target: powershell.exe -File “C:\Path\To\ZipSubfolders.ps1”
Note! You will need to set your Powershell Execution Policy to allow this to work.
Then drag & drop the desired folder onto the shortcut and all its subfolders will be compressed to individual .zip files named after the folder. Folders that already have a corresponding zip file will be skipped.
1. Compress using Powershell and Windows Compression
# Script to Zip all the subfolders in a particular folder
# Create a Shortcut using the target: powershell.exe -File "C:\Path\To\ZipSubfolders.ps1"
param (
[string]$parentFolderPath
)
if (-not $parentFolderPath) {
Write-Host "Please drag and drop a folder onto the script."
exit
}
Write-Host "Using folder path: $parentFolderPath"
# Get all subfolders in the specified folder
$subfolders = Get-ChildItem -Path $parentFolderPath -Directory
foreach ($subfolder in $subfolders) {
$zipFileName = "$($subfolder.FullName).zip"
$subfolderPath = $subfolder.FullName
# Skip if zip already exists
if (Test-Path $zipFileName) {
Write-Output "Zip file already exists: $zipFileName. Skipping..."
continue
}
# Count files and calculate size before zipping
$files = Get-ChildItem -Path $subfolderPath -File -Recurse
$fileCount = $files.Count
$totalSize = ($files | Measure-Object -Property Length -Sum).Sum
# Create zip using Windows built-in Compress-Archive
Compress-Archive -Path $subfolderPath -DestinationPath $zipFileName
# Output summary line
Write-Output "Created zip: $zipFileName | Files: $fileCount | Size: $([math]::Round($totalSize / 1MB, 2)) MB"
}
Write-Output "All subfolders have been zipped using Windows Compress-Archive."
Pause
2. Compress using Powershell and 7-zip (Faster)
# Script to Zip all the subfolders in a particular folder
# !!! This version depends on 7-zip !!!
# Create a Shortcut using the target: powershell.exe -File "C:\Path\To\ZipSubfolders.ps1"
param (
[string]$parentFolderPath
)
if (-not $parentFolderPath) {
Write-Host "Please drag and drop a folder onto the script."
exit
}
Write-Host "Using folder path: $parentFolderPath"
# Get all subfolders in the specified folder
$subfolders = Get-ChildItem -Path $parentFolderPath -Directory
# Loop through each subfolder and create a zip file using 7-Zip
foreach ($subfolder in $subfolders) {
$zipFileName = "$($subfolder.FullName).zip"
$subfolderPath = $subfolder.FullName
# Check if the zip file already exists
if (Test-Path $zipFileName) {
Write-Output "Zip file already exists: $zipFileName. Skipping..."
continue
}
# Run 7-Zip and capture the output
$output = & "C:\Program Files\7-Zip\7z.exe" a $zipFileName $subfolderPath 2>&1
# Extract relevant information from the output
$filesRead = ($output | Select-String -Pattern "Files read from disk: (\d+)").Matches[0].Groups[1].Value
$archiveSize = ($output | Select-String -Pattern "Archive size: (\d+) bytes").Matches[0].Groups[1].Value
# Write confirmation to output
Write-Output "Created zip file: $zipFileName, Files: $filesRead, Size: $archiveSize bytes"
}
Write-Output "All subfolders have been zipped using 7-Zip."
Pause
Add to Windows Explorer Context Menu
The following registry script will add context menu items for first script. Be sure to set the path appropriately.
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\Zip Subfolders] @="Zip All Subfolders" [HKEY_CLASSES_ROOT\Directory\shell\Zip Subfolders\command] @="powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"C:\\Path\\To\\Script\\Subfolders_Zip.ps1\" -parentFolderPath \"%1\""