Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip and Unzip File in Powershell 4

I am using Windows Server 2012 R2 (64 bit). I have powershell version 4 available in it. I am trying to zip and unzip files. When I try Write-Zip command, it throws me following error:

Write-Zip : The term 'Write-Zip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What should I do to fix it? Do I need to install zip/winrar in the server? Or is there any other command do zip/unzip files?

like image 889
Raji Avatar asked Nov 19 '16 10:11

Raji


People also ask

How do I unzip a file in PowerShell?

Zip and unzip with PowerShell. The cmdlet Compress-Archive and Expand-Archive have been introduced in PowerShell 5 and allow you to easily zip (compress) and unzip (extract) archives on the the command line.

How to extract files from a zipped archive in PowerShell?

The following commands are two of the newest cmdlets released in PowerShell v5.0: The Compress-Archive command creates a zipped archive file from folders or individual files and the Expand-Archive command to extract files from a zipped archive file. Let’s use PowerShell to compress files in a new .zip archive.

How do I extract files from a zip file in Windows?

To do so, click on the Start button and then type in “PowerShell” and select the first application shown. Make sure to change the <path-to-files> to the actual path of the zip file and <path-to-destinatiom> with the location where you want to save extracted files.

How do i compress a file in PowerShell?

Microsoft has added command line–driven compression to PowerShell in the latest release through two cmdlets: Compress-Archive – used to create compressed (zip) files. Expand-Archive – used to extract files from their zip file containers.


2 Answers

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

like image 127
kim Avatar answered Sep 18 '22 13:09

kim


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.

like image 27
spuder Avatar answered Sep 18 '22 13:09

spuder