{"id":7351,"date":"2015-03-11T00:01:00","date_gmt":"2015-03-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/11\/use-powershell-to-extract-zipped-files\/"},"modified":"2019-02-18T10:30:19","modified_gmt":"2019-02-18T17:30:19","slug":"use-powershell-to-extract-zipped-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-extract-zipped-files\/","title":{"rendered":"Use PowerShell to Extract Zipped Files"},"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 extract zipped files.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>\u00a0Hey, Scripting Guy! I need to be able to work with zipped files from time-to-time. Often I store files in a zip archive so they are portable. I know how to copy the .zip archive from one place to another with Windows PowerShell, but I cannot seem to figure out how to unzip the archive. Can you help me? I would be ever so grateful.\n\u2014SK\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0Hello SK,\nMicrosoft Scripting Guy, Ed Wilson, is here. This morning I got out my bag of espresso beans and set up my rotary bean grinder. I am thinking I will make some espresso this afternoon\u2026or maybe tomorrow afternoon. I have an old-fashioned, stovetop, double-boiler. It is like one I bought a long time ago when I was in Naples, Italy. It is ridiculously simple, and does an excellent job. I put the water in the bottom and super finely ground beans in the middle, and after a short time, the espresso appears in the top. The only trick is ensuring that I get the right amount of water in the bottom.\nI sometimes also make cappuccinos on Saturday mornings, and I have a hand milk frothier that I use for that. I love using manual tools when I have time, or when I want to take time for an exceptional occurrence. However, for things that occur more than once or twice a month, I want to automate them\u2014big time. Maybe one day, someone will invent a PowerShell powered teapot.\nTo extract all files from a .zip archive file, I use the <b>ExtractToDirectory<\/b> static method from the [io.compression.zipfile] .NET Framework class. To use this class, I need to add the System.IO.Compression.FileSystem assembly to my Windows PowerShell console or to the Windows PowerShell ISE.\nTo add the assembly, I use the <b>Add-Type<\/b> cmdlet and specify the <b>\u2013Assembly<\/b> parameter. This command is shown here:<\/p>\n<p style=\"margin-left: 30px\">Add-Type -assembly &#8220;system.io.compression.filesystem&#8221;<\/p>\n<p>The command to extract the zipped files to a folder is:<\/p>\n<p style=\"margin-left: 30px\">[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)<\/p>\n<p>Here are a few things to keep in mind:<\/p>\n<ol>\n<li>The first parameter I call ($<b>BackUpPath<\/b>)<b> <\/b>must point to a specific zipped file.<\/li>\n<li>The second parameter (the one I call <b>$destination<\/b>) must point to a folder.<\/li>\n<li>Both of these parameters are strings. Therefore, I cannot use a <b>ziparchive<\/b> object or a <b>directoryinfo<\/b> object as input types.<\/li>\n<li>The extraction does not include the root folder.<\/li>\n<\/ol>\n<p>My complete script is shown here:<\/p>\n<p style=\"margin-left: 30px\">$BackUpPath = &#8220;C:backupfso.zip&#8221;<\/p>\n<p style=\"margin-left: 30px\">$Destination = &#8220;C:recovered&#8221;<\/p>\n<p style=\"margin-left: 30px\">Add-Type -assembly &#8220;system.io.compression.filesystem&#8221;<\/p>\n<p style=\"margin-left: 30px\">[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)<\/p>\n<p>When I go to my C:recovered folder, I see that all of the files from the fso.zip folder are now present.\nSK, that is all there is to using Windows PowerShell to extract zipped files. Zip Week will continue tomorrow when I will talk about zipping and emailing an archived folder.\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size: 12px\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to extract zipped files. \u00a0Hey, Scripting Guy! I need to be able to work with zipped files from time-to-time. Often I store files in a zip archive so they are portable. I know how to copy the .zip archive from one place to another [&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-7351","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: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to extract zipped files. \u00a0Hey, Scripting Guy! I need to be able to work with zipped files from time-to-time. Often I store files in a zip archive so they are portable. I know how to copy the .zip archive from one place to another [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7351","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=7351"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7351\/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=7351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}