Thursday, 9 June 2016

How to Zip all files in a folder with Power Shell

How to Zip all files in a folder with Power Shell

I hope below script will help us to zip file in a particular format. 


###Powershell Start
if (-not (test-path "C:\Program Files\7-Zip\7z.exe")) {throw "C:\Program Files\7-Zip\7z.exe needed"}
set-alias sz "C:\Program Files\7-Zip\7z.exe" | Out-Null


#### Variables 

$filePath = "f:\t"
$fileEXT = ".txt"
$ziptype = ".7z"

$bak = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq $fileEXT }


foreach ($file in $bak) {
                    $name = $file.name
                    $directory = $file.DirectoryName
                    $zipfile = $name.Replace($fileEXT,$ziptype)
                    sz a -tzip "$directory\$zipfile" "$directory\$name"      
                }


###Powershell END

No comments:

Post a Comment