Create zip/compressed folders with Powershell

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)} 

Posted 08-07-2006 2:59 PM by mhodnick
Filed under:

Comments

Karsten Januszewski wrote re: Create zip/compressed folders with Powershell
on 09-18-2006 3:21 PM
Hmm, the first sample, where you ls, only seems to grab the first file and doesn't walk each file.  Any ideas?
Mike Hodnick wrote PowerShell: Zip up a folder and email it
on 11-29-2006 11:49 AM
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 wrote PowerShell News and Examples HQ » Blog Archive » Create zip/compressed folders with Powershell
on 11-30-2006 6:07 AM
Mike Hodnick wrote PowerShell: Zip up a folder and email it
on 05-31-2007 8:42 AM

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

Zipping Files with PowerShell « devioblog wrote Zipping Files with PowerShell « devioblog
on 02-11-2009 12:47 PM

Pingback from  Zipping Files with PowerShell « devioblog

Zipping Files with PowerShell « devioblog wrote Zipping Files with PowerShell « devioblog
on 02-11-2009 12:52 PM

Pingback from  Zipping Files with PowerShell « devioblog

powershell and 7zip | keyongtech wrote powershell and 7zip | keyongtech
on 03-14-2009 1:09 PM

Pingback from  powershell and 7zip | keyongtech

Inetium, LLC. Site Information