Can you zip a folder from the Windows command-line without installing anything? Yes, with PowerShell and .NET 4.5

Posted: (EET/GMT+2)

 

Sometimes, you need to work from the command-line (as in Windows Server Core), or you want to automate zipping a folder into a .ZIP file. If you cannot or don't want to install any additional tools, you can rely on PowerShell and .NET 4.5's ZipFile class. The ZipFile class is part of the namespace System.IO.Compression, and implemented in the assembly System.IO.Compression.FileSystem.dll.

If you have a source folder "C:\Data\Stuff" and you want to create a zip file from this folder into "C:\Backup\Stuff.zip", here's what you would run from a PowerShell prompt:

Add-Type -AssemblyName "System.IO.Compression.FileSystem"

[IO.Compression.ZipFile]::CreateFromDirectory("C:\Data\Stuff","C:\Backup\Stuff.zip")

For this to work, the computer must have .NET 4.5 or later installed.

If you instead want to extract a zip file into a certain folder, you can use the ExtractToDirectory method instead.

Happy zippin'!