{"id":7371,"date":"2015-03-10T00:01:00","date_gmt":"2015-03-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/10\/use-powershell-to-zip-multiple-folders\/"},"modified":"2019-02-18T10:30:20","modified_gmt":"2019-02-18T17:30:20","slug":"use-powershell-to-zip-multiple-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-zip-multiple-folders\/","title":{"rendered":"Use PowerShell to Zip Multiple Folders"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Use Windows PowerShell to create a .zip archive of multiple folders.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I need to compress multiple folders before I attempt to archive them. I would like to do this without having to install additional software. Can you help?<\/p>\n<p>&mdash;DR<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello DR,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I am sipping a red berry tea and munching on a chocolate biscotti. Maybe it is not too exciting, but it is relaxing. I am looking over my email sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> on my Surface 3 Pro, and things are good.<\/p>\n<p>One of the cool things about the free update to Windows 8.1 from Windows 8 is that in addition to including Windows PowerShell&nbsp;4.0, it includes .NET Framework 4.5, which is way cool. The thing I love the best is the improved compression classes. It makes working with .zip files a piece of cake.<\/p>\n<p>I have a folder on my laptop that I use for backing up files, creating archives, and stuff like that. So, I do not need to check to see if a folder exists or worry about overwriting such a folder. Here is the path assignment in my script:<\/p>\n<p style=\"margin-left:30px\">$path = &quot;C:\\backup&quot;<\/p>\n<p>I use the <b>Get-ChildItem<\/b> cmdlet to find all of the folders I want to archive. In this example, I want to archive all of my FSO* types of folders. I test my command before I add it to my script. This is the command and its output:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-ChildItem -Path c:\\ -Filter &quot;fso?&quot; -Directory<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\<\/p>\n<p style=\"margin-left:30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">d&#8212;-&nbsp;&nbsp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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3\/9\/2015&nbsp;&nbsp; 3:28 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fso3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>The cool thing is that in the Windows PowerShell ISE, I can highlight only the portion of the command I want to use, and that is what runs. So my actual command will be:<\/p>\n<p style=\"margin-left:30px\">$source = Get-ChildItem -Path c:\\ -Filter &quot;fso?&quot; -Directory<\/p>\n<p>I know that this returns a <b>DirectoryInfo<\/b> object, and that I need to access specific properties to get to the individual folder paths&mdash;but I will do that later.<\/p>\n<p>I need to add the assembly that contains the compress classes, so I do this here:<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &quot;system.io.compression.filesystem&quot;<\/p>\n<p>I now need to create the destination path for each archive I will create. I do this inside a loop that walks through my collection of <b>DirectoryInfo<\/b> objects. This script is shown here:<\/p>\n<p style=\"margin-left:30px\">Foreach ($s in $source)<\/p>\n<p style=\"margin-left:30px\">&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp; $destination = Join-path -path $path -ChildPath &quot;$($s.name).zip&quot;<\/p>\n<p>I keep only one archive of a folder in my Backup folder at a time, so if the archive exists, I want to delete it. Here is the script that accomplishes that task:<\/p>\n<p style=\"margin-left:30px\">If(Test-path $destination) {Remove-item $destination}<\/p>\n<p>Now it is the simple task of creating the archive. Here is the command:<\/p>\n<p style=\"margin-left:30px\">[io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">$path = &quot;C:\\backup&quot;<\/p>\n<p style=\"margin-left:30px\">$source = Get-ChildItem -Path c:\\ -Filter &quot;fso?&quot; -Directory<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &quot;system.io.compression.filesystem&quot;<\/p>\n<p style=\"margin-left:30px\">Foreach ($s in $source)<\/p>\n<p style=\"margin-left:30px\">&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp; $destination = Join-path -path $path -ChildPath &quot;$($s.name).zip&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp; If(Test-path $destination) {Remove-item $destination}<\/p>\n<p style=\"margin-left:30px\">&nbsp; [io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)}<\/p>\n<p>I check to see if the archives exist. As shown in the following image, they do:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-10-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-10-15-01.png\" alt=\"Image of folder\" title=\"Image of folder\" \/><\/a><\/p>\n<p>DR, that is all there is to using Windows PowerShell to create a .zip archive of multiple folders. Zip Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to create a .zip archive of multiple folders. &nbsp;Hey, Scripting Guy! I need to compress multiple folders before I attempt to archive them. I would like to do this without having to install additional software. Can you help? &mdash;DR &nbsp;Hello DR, Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[567,3,12,45],"class_list":["post-7371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files-and-folders","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to create a .zip archive of multiple folders. &nbsp;Hey, Scripting Guy! I need to compress multiple folders before I attempt to archive them. I would like to do this without having to install additional software. Can you help? &mdash;DR &nbsp;Hello DR, Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7371"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7371\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=7371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}