How to zip files and folders using powershell?

I want to zip Folder2 and Folder4 (specific folders) with subfolders and files using powershell. Any help is appreciated.

Folder1
   Folder2
            Folder7
            Folder8
  Folder3
  Folder4
           Folder9
           Folder10
  Folder5
  Folder6 

June 28th, 2013 5:16pm

$zip = "c:\test\test.zip"
New-Item $zip -ItemType file
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zip)
$files = gci c:\test\* -Exclude "*.zip" -Recurse
	
foreach($file in $files) 
	{ 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
	}

try something like this

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 5:35pm

From what I've seen in other questions, using the Shell.Application object in a script leads to problems (because it's always asynchronous, not intended for scripting).  I did some searching and found a PowerShell sample that uses the .NET System.IO.Packaging.ZipPackage class instead:

http://www.winsoft.se/2010/12/easy-archiving-of-files-using-powershell/

If you're using PowerShell v3 (.NET Framework 4 is required), it's apparently even easier (found this function in the comments of this post:  http://stackoverflow.com/questions/1153126/how-to-create-a-zip-archive-with-powershell

function ZipFiles( $zipfilename, $sourcedir )
{
    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory( $sourcedir, $zipfilename, $compressionLevel, $false )
}


June 28th, 2013 5:54pm

From what I've seen in other questions, using the Shell.Application object in a script leads to problems (because it's always asynchronous, not intended for scripting).  I did some searching and found a PowerShell sample that uses the .NET System.IO.Packaging.ZipPackage class instead:

http://www.winsoft.se/2010/12/easy-archiving-of-files-using-powershell/

If you're using PowerShell v3 (.NET Framework 4 is required), it's apparently even easier (found this function in the comments of this post:  http://stackoverflow.com/questions/1153126/how-to-create-a-zip-archive-with-powershell

function ZipFiles( $zipfilename, $sourcedir )
{
    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory( $sourcedir, $zipfilename, $compressionLevel, $false )
}


Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 5:54pm

there are problems with unzipping, havent seen problems zipping yet
June 28th, 2013 6:02pm

It's a pretty rough question that comes up regularly.  In order to use the zip function that david mentioned you will need Powershell version 3 and .Net 4.5

I still use 7zip's Command line interface.  It's the easiest option I found so far.

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 7:11pm

Thank you ImMax. I have tried using the code and it does not maintain the folder structure. All folders and files are in the root folder. Any idea?
June 30th, 2013 6:24pm

Jason,

Installed 7-zip, I want to zip Folder2 and Folder4 (specific folders) with subfolders and files using 7-zip. How can I specify multiple folders on the commandline? How you do that?
 
Folder1
    Folder2
             Folder7
             Folder8
   Folder3
   Folder4
            Folder9
            Folder10
   Folder5
   Folder6

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 4:37pm

Wit the shell.application and the .NET 4 way to zip you have to maintain the folder structure by your self!
This is very hard to code correctly ! You will dislike it! ;-)

I suggest you go with tested tools like 7zip! As R jason Morgen suggested!

Event the PowerShell community Module (Pscx)) uses 7Zip under the hood!
http://pscx.codeplex.com/

http://thomasfreudenberg.com/blog/archive/2013/04/07/7-zip-for-powershell.aspx

http://gallery.technet.microsoft.com/scriptcenter/PowerShell-and-7Zip-83020e74

July 2nd, 2013 1:25am

Hi,

Have tried above suggestions? Any update about the issue?

Please let us know if you would like further assistance.

 


Regards,

Yan Li

If you have any feedback on our support, please click here .

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 1:44am

That's perfect - thanks!

July 31st, 2014 11:42pm

PowerShell 5.0 has a new cmdlet for this:

Compress-Archive .\file1.txt, .\file2.txt -DestinationPath .\files.zip
Free Windows Admin Tool Kit Click here and download it now
September 17th, 2014 10:51pm

Be warned, as of right now WMF 5.0 is still in preview only.
September 17th, 2014 10:58pm

BETA warning still applies, but for what it's worth, 

As of the Feb 19 release, this PS functionality has reached "stable" status (though the whole WMF is still beta)

Scenario

Design Status

32-bit support for the configuration keyword in DSC

Stable

Generate Windows PowerShell cmdlets based on an OData endpoint with ODataUtils

Stable

Manage .ZIP archives through new cmdlets

Stable

Audit Windows PowerShell usage by transcription and logging

Stable

Interact with symbolic links using improved Item cmdlets

Stable

Network Switch management with Windows PowerShell

Stable

DSC authoring improvements in Windows PowerShell ISE

Stable

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2015 4:07pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics