Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

i have found this powershell script online and i would like to run this from a batchfile. Can anyone give me the correct syntax for this script. And if possible make it possible to input multiple directories the script wil zip.

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

So what I actually need is the cmd file that says this (preferably with extra folders)

gi c:\source | out-zip c:\target\archive.zip $_

Thanks, Kim!

share|improve this question
    
Is this where you found that example? –  Justin Dearing Aug 12 '12 at 14:04
    
yes, among various other places –  Kim Aug 12 '12 at 15:41

2 Answers 2

I had the same problem with only a 1kb zip file being created. I found that I had do include a -noexit in the command: powershell.exe -noexit -Command "gi c:\source | C:\users\Kim\Dpcuments\WindowsPowerShell\out-zip.ps1 c:\target\archive.zip"

Granted, this seems to cause some other issues when trying to use this in a batch file because it leaves powershell open. There are some other solutions out there that I'm investigating that will either just wait for a set amount of time for the "zip" to complete or that actually compares the source files with the completed zip file before exiting.

share|improve this answer

I cleaned up the PowerShell script file for you:

<#
.SYNOPSIS
Zip up files and folders
.EXAMPLE
To zip up some files:
    ls c:\source\*.txt | out-zip.ps1 c:\target\archive.zip
.EXAMPLE
To zip up a folder:
    gi c:\source | out-zip c:\target\archive.zip
#>

param(
    [parameter(Mandatory=$TRUE,ValueFromPipeline=$TRUE)] $files,
    [parameter(Mandatory=$TRUE,position=0)] [string] $path
)

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([System.IO.Path]::GetFullPath($path)) 
$files | %{$zipfile.CopyHere($_.fullname)}

Assuming out-zip.ps1 is saved to C:\users\Kim\Dpcuments\WindowsPowerShell\out-zip.ps1, below is the batch file you need.

@echo off
powershell.exe -Command "gi c:\source | C:\users\Kim\Dpcuments\WindowsPowerShell\out-zip.ps1 c:\target\archive.zip"
share|improve this answer
    
Thanks Justin, tried it but it doesn't work. It creates a zip file but it's empty. –  Kim Aug 12 '12 at 9:19
    
@Kim are you trying to zip up C:\source or another folder? –  Justin Dearing Aug 12 '12 at 13:13
    
@Kim you can lose the $_'s with my improved version of the script. –  Justin Dearing Aug 12 '12 at 14:06
    
i am backing up c:\test and the ps1 file is in c:\backup\, this is my code: powershell.exe -Command "gi c:\test | C:\backup\out-zip.ps1 c:\backup\archive.zip" –  Kim Aug 12 '12 at 15:45
    
I have tried the code but it still returns a 1kb zip file (empty), did you manage a succesful zip? –  Kim Aug 12 '12 at 15:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.