Zip folder with powershell

02/02/2013 05:51

Check if get-item is folder or file

$file.PSIsContainer #property is true or false

 

#Zip folder  with files or directories

 

$installationFilesFolderPath = "c:\temp"

#Note: if you use -Recurse in dir command, files will be there multiple times, don't use it here.

dir $installationFilesFolderPath | Add-Zip ($installationFilesFolderPath + ".zip")

 

function Add-Zip

{

param([string]$zipfilename)

if(-not (test-path($zipfilename)))

{

set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

(dir $zipfilename).IsReadOnly = $false

}

$shellApplication = new-object -com shell.application

$zipPackage = $shellApplication.NameSpace($zipfilename)

foreach($file in $input)

{

$zipPackage.CopyHere($file.FullName)

Start-sleep -milliseconds 500

 

}

}