Zip and Unzip File in Powershell 4

You can use custom powershell object New-Object -ComObject Shell.Application and copy the file with flags to unzip.

$filePath = "foo.zip"
$shell = New-Object -ComObject Shell.Application
$zipFile = $shell.NameSpace($filePath)
$destinationFolder = $shell.NameSpace("C:\Program Files\WindowsPowerShell\Modules")

$copyFlags = 0x00
$copyFlags += 0x04 # Hide progress dialogs
$copyFlags += 0x10 # Overwrite existing files

$destinationFolder.CopyHere($zipFile.Items(), $copyFlags)

Credit source https://github.com/hashicorp/best-practices/blob/master/packer/scripts/windows/install_windows_updates.ps1#L12-L22

This does not work with windows 'core' edition. If possible, upgrade to powershell 5 and use Expand Archive since it is much simpler.


Write-Zip seems to be part of http://pscx.codeplex.com/ that require a separate installation before you can use it.

However, if you just want to create a Zip archive from a folder, you could just run

$source = "c:\temp\source"
$archive = "c:\temp\archive.zip"

Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($source, $archive)

This utilizes the CreateFromDirectory method from the .NET Framework class ZipFile. It creates a zip archive from the files located inside the $source folder and creates an archive as defined in the $archive variable. Note, ZipFile class was introduced in .NET Framework 4.5