{"id":386,"date":"2025-10-30T10:30:05","date_gmt":"2025-10-30T16:30:05","guid":{"rendered":"https:\/\/blog.d5.ca\/?p=386"},"modified":"2026-01-12T15:52:00","modified_gmt":"2026-01-12T22:52:00","slug":"compress-all-subfolders-to-zip-files","status":"publish","type":"post","link":"https:\/\/blog.d5.ca\/?p=386","title":{"rendered":"Compress all Subfolders to Zip Files"},"content":{"rendered":"<p>Have you ever wanted to compress all the subfolders in a specific folder to individual Zip files? <!--more--><\/p>\n<p>There are two Powershell scripts below. To use them, create a shortcut to the script using the following command in the shortcut target: <em>powershell.exe -File &#8220;C:\\Path\\To\\ZipSubfolders.ps1&#8221;<\/em><\/p>\n<p>Note! You will need to set your Powershell <a href=\"https:\/\/ss64.com\/ps\/set-executionpolicy.html\" target=\"_blank\" rel=\"noopener\">Execution Policy<\/a> to allow this to work.<\/p>\n<p>Then drag &amp; 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.<\/p>\n<h3>1. Compress using Powershell and Windows Compression<\/h3>\n<pre># Script to Zip all the subfolders in a particular folder\r\n# Create a Shortcut using the target: powershell.exe -File \"C:\\Path\\To\\ZipSubfolders.ps1\"\r\n\r\nparam (\r\n[string]$parentFolderPath\r\n)\r\n\r\nif (-not $parentFolderPath) {\r\nWrite-Host \"Please drag and drop a folder onto the script.\"\r\nexit\r\n}\r\n\r\nWrite-Host \"Using folder path: $parentFolderPath\"\r\n\r\n# Get all subfolders in the specified folder\r\n$subfolders = Get-ChildItem -Path $parentFolderPath -Directory\r\n\r\nforeach ($subfolder in $subfolders) {\r\n$zipFileName = \"$($subfolder.FullName).zip\"\r\n$subfolderPath = $subfolder.FullName\r\n\r\n# Skip if zip already exists\r\nif (Test-Path $zipFileName) {\r\nWrite-Output \"Zip file already exists: $zipFileName. Skipping...\"\r\ncontinue\r\n}\r\n\r\n# Count files and calculate size before zipping\r\n$files = Get-ChildItem -Path $subfolderPath -File -Recurse\r\n$fileCount = $files.Count\r\n$totalSize = ($files | Measure-Object -Property Length -Sum).Sum\r\n\r\n# Create zip using Windows built-in Compress-Archive\r\nCompress-Archive -Path $subfolderPath -DestinationPath $zipFileName\r\n\r\n# Output summary line\r\nWrite-Output \"Created zip: $zipFileName | Files: $fileCount | Size: $([math]::Round($totalSize \/ 1MB, 2)) MB\"\r\n}\r\n\r\nWrite-Output \"All subfolders have been zipped using Windows Compress-Archive.\"\r\nPause<\/pre>\n<h3>2. Compress using Powershell and 7-zip (Faster)<\/h3>\n<pre># Script to Zip all the subfolders in a particular folder\r\n# !!! This version depends on 7-zip !!!\r\n# Create a Shortcut using the target: powershell.exe -File \"C:\\Path\\To\\ZipSubfolders.ps1\"\r\n\r\nparam (\r\n[string]$parentFolderPath\r\n)\r\n\r\nif (-not $parentFolderPath) {\r\nWrite-Host \"Please drag and drop a folder onto the script.\"\r\nexit\r\n}\r\n\r\nWrite-Host \"Using folder path: $parentFolderPath\"\r\n\r\n# Get all subfolders in the specified folder\r\n$subfolders = Get-ChildItem -Path $parentFolderPath -Directory\r\n\r\n# Loop through each subfolder and create a zip file using 7-Zip\r\nforeach ($subfolder in $subfolders) {\r\n$zipFileName = \"$($subfolder.FullName).zip\"\r\n$subfolderPath = $subfolder.FullName\r\n\r\n# Check if the zip file already exists\r\nif (Test-Path $zipFileName) {\r\nWrite-Output \"Zip file already exists: $zipFileName. Skipping...\"\r\ncontinue\r\n}\r\n\r\n# Run 7-Zip and capture the output\r\n$output = &amp; \"C:\\Program Files\\7-Zip\\7z.exe\" a $zipFileName $subfolderPath 2&gt;&amp;1\r\n\r\n# Extract relevant information from the output\r\n$filesRead = ($output | Select-String -Pattern \"Files read from disk: (\\d+)\").Matches[0].Groups[1].Value\r\n$archiveSize = ($output | Select-String -Pattern \"Archive size: (\\d+) bytes\").Matches[0].Groups[1].Value\r\n\r\n# Write confirmation to output\r\nWrite-Output \"Created zip file: $zipFileName, Files: $filesRead, Size: $archiveSize bytes\"\r\n}\r\n\r\nWrite-Output \"All subfolders have been zipped using 7-Zip.\"\r\nPause<\/pre>\n<h2>Add to Windows Explorer Context Menu<\/h2>\n<p>The following registry script will add context menu items for first script. Be sure to set the path appropriately.<\/p>\n<pre>Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CLASSES_ROOT\\Directory\\shell\\Zip Subfolders]\r\n@=\"Zip All Subfolders\"\r\n\r\n[HKEY_CLASSES_ROOT\\Directory\\shell\\Zip Subfolders\\command]\r\n@=\"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \\\"C:\\\\Path\\\\To\\\\Script\\\\Subfolders_Zip.ps1\\\" -parentFolderPath \\\"%1\\\"\"<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to compress all the subfolders in a specific folder to individual Zip files?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[25],"tags":[68,66,63,64,42,67,65],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-reference","tag-context-menu","tag-explorer","tag-folders","tag-powershell","tag-registry","tag-shell-extensions","tag-zip"],"_links":{"self":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386"}],"version-history":[{"count":4,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":393,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions\/393"}],"wp:attachment":[{"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.d5.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}