{"id":7391,"date":"2015-03-09T00:01:00","date_gmt":"2015-03-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/09\/use-powershell-to-create-zip-archive-of-folder\/"},"modified":"2019-02-18T10:30:21","modified_gmt":"2019-02-18T17:30:21","slug":"use-powershell-to-create-zip-archive-of-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-zip-archive-of-folder\/","title":{"rendered":"Use PowerShell to Create ZIP Archive of Folder"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a .zip archive of a folder.<\/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 a way to create a .zip archive of a folder. I would like to do this on my laptop running Windows&nbsp;8.1, and I do not want to install any other software. Can I do this?<\/p>\n<p>&mdash;TR<\/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 TR,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The weed eater dude is outside. The guy is really dedicated to his job. I mean, the snow has barely cleared, and he is out there chopping away with his weed eater. I really enjoy hearing him, because it fills me with hope that summer is on its way, and that soon we will have warm weather and we can get outside without having to bundle up.<\/p>\n<p>Certainly I can put on a coat and hop on my bicycle, but I learned (the hard way) a long time ago that trying to ride a bicycle when there is ice on the road is not the smartest thing to do (at least not for me). So I prefer to wait until the snow melts, the ice thaws, and the sun is out before taking to the open road.<\/p>\n<p>TR, luckily, you do not need to wait for anything before you can use Windows PowerShell to create a .zip archive. You have everything you need&mdash;and that is .NET Framework&nbsp;4.5.<\/p>\n<p>The <b>ZipFile<\/b> .NET Framework class was introduced with .NET Framework&nbsp;4.5, and Windows 8.1 ships with .NET Framework 4.5 installed. This is really cool because this class is extremely easy to use.<\/p>\n<p>The <b>ZipFile<\/b> class is not available by default in Windows PowerShell because the System.IO.Compression.FileSystem assembly is not loaded by default. Therefore, I want to load the assembly. The best way to do this is to use the <b>Add-Type<\/b> cmdlet and specify the name of the assembly as an argument to the <b>&ndash;Assembly<\/b> parameter. This is the command:<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &quot;system.io.compression.filesystem&quot;<\/p>\n<p>The <b>ZipFile<\/b> .NET Framework class has a static method named <b>CreateFromDirectory<\/b>. This method accepts two arguments: a source directory and a destination file. The source directory and the destination file cannot reside in the same directory. This is due to file locking, and it will generate the following &nbsp;error message:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-9-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-9-15-01.png\" alt=\"Image of error message\" title=\"Image of error message\" \/><\/a><\/p>\n<p>When you are using this method, if the file archive file already exists, the following error message appears:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-9-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-9-15-02.png\" alt=\"Image of error message\" title=\"Image of error message\" \/><\/a><\/p>\n<p>To fix this issue, I add a <b>Test-Path<\/b> command to delete the archive file if it already exists. This is shown here:<\/p>\n<p style=\"margin-left:30px\">If(Test-path $destination) {Remove-item $destination}<\/p>\n<p>The <b>CreateFromDirectory<\/b> method is easy to use. It is a static method, so I can access it directly from the <b>ZipFile<\/b> class. Also it takes two arguments, <b>Source<\/b> and <b>Destination<\/b>, so the method call makes sense. Here is the command I use:<\/p>\n<p style=\"margin-left:30px\">[io.compression.zipfile]::CreateFromDirectory($Source, $destination)<\/p>\n<p>For the complete script, I add <b>Source<\/b> and <b>Destination<\/b> as variables at the beginning of the script. Remember, <b>Source<\/b> is a directory and <b>Destination<\/b> is a file that will hold my .zip archive. Here is the complete script:<\/p>\n<p style=\"margin-left:30px\">$source = &quot;C:\\fso&quot;<\/p>\n<p style=\"margin-left:30px\">$destination = &quot;C:\\fso1\\FSO_Backup.zip&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;If(Test-path $destination) {Remove-item $destination}<\/p>\n<p style=\"margin-left:30px\">Add-Type -assembly &quot;system.io.compression.filesystem&quot;<\/p>\n<p style=\"margin-left:30px\">[io.compression.zipfile]::CreateFromDirectory($Source, $destination)&nbsp;<\/p>\n<p>Now when I run the script, an archive appears in my destination folder. The archive contains all of the files from the source. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-9-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-9-15-03.png\" alt=\"Image of folder\" title=\"Image of folder\" \/><\/a><\/p>\n<p>TR, that is all there is to using Windows PowerShell to create a .zip archive of a folder. 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: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a .zip archive of a folder. &nbsp;Hey, Scripting Guy! I need a way to create a .zip archive of a folder. I would like to do this on my laptop running Windows&nbsp;8.1, and I do not want to install any other software. [&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":[11,3,12,45],"class_list":["post-7391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a .zip archive of a folder. &nbsp;Hey, Scripting Guy! I need a way to create a .zip archive of a folder. I would like to do this on my laptop running Windows&nbsp;8.1, and I do not want to install any other software. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7391","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=7391"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7391\/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=7391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}