Create zip/compressed folders with Powershell

Posted Monday, August 07, 2006 2:59 PM by mhodnick

I came across this Powershell blog post about adding folders and files to a zip archive, but it wasn't exactly 100% usable out of the box.  Here's a slight re-work of the script that will have you zipping files from a prompt all day and all night:


########################################################
#
# out-zip.ps1
#
# Usage:
# 
# To zip up some files:
# ls c:\source\*.txt | out-zip c:\target\archive.zip $_
#
# To zip up a folder:
# gi c:\source | out-zip c:\target\archive.zip $_
########################################################

$path = $args[0]
$files = $input
  
if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

if (-not (test-path $path)) { 
  set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($path) 
$files | foreach {$zipfile.CopyHere($_.fullname)} 
Filed under:

Comments

# re: Create zip/compressed folders with Powershell

Monday, September 18, 2006 3:21 PM by Karsten Januszewski

Hmm, the first sample, where you ls, only seems to grab the first file and doesn't walk each file.  Any ideas?

# PowerShell: Zip up a folder and email it

Wednesday, November 29, 2006 11:49 AM by Mike Hodnick

Here's a quick little PowerShell script you can use in conjunction with Out-Zip to zip up a folder on...

# PowerShell News and Examples HQ » Blog Archive » Create zip/compressed folders with Powershell

# PowerShell: Zip up a folder and email it

Thursday, May 31, 2007 8:42 AM by Mike Hodnick

Here's a quick little PowerShell script you can use in conjunction with Out-Zip to zip up a folder on