March 9th, 2015

Use PowerShell to Create ZIP Archive of Folder

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create a .zip archive of a folder.

Hey, Scripting Guy! Question 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 8.1, and I do not want to install any other software. Can I do this?

—TR

Hey, Scripting Guy! Answer Hello TR,

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.

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.

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—and that is .NET Framework 4.5.

The ZipFile .NET Framework class was introduced with .NET Framework 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.

The ZipFile 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 Add-Type cmdlet and specify the name of the assembly as an argument to the –Assembly parameter. This is the command:

Add-Type -assembly "system.io.compression.filesystem"

The ZipFile .NET Framework class has a static method named CreateFromDirectory. 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  error message:

Image of error message

When you are using this method, if the file archive file already exists, the following error message appears:

Image of error message

To fix this issue, I add a Test-Path command to delete the archive file if it already exists. This is shown here:

If(Test-path $destination) {Remove-item $destination}

The CreateFromDirectory method is easy to use. It is a static method, so I can access it directly from the ZipFile class. Also it takes two arguments, Source and Destination, so the method call makes sense. Here is the command I use:

[io.compression.zipfile]::CreateFromDirectory($Source, $destination)

For the complete script, I add Source and Destination as variables at the beginning of the script. Remember, Source is a directory and Destination is a file that will hold my .zip archive. Here is the complete script:

$source = "C:\fso"

$destination = "C:\fso1\FSO_Backup.zip"

 If(Test-path $destination) {Remove-item $destination}

Add-Type -assembly "system.io.compression.filesystem"

[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 

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:

Image of folder

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.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.