Powershell

DotNetZip

You can use DotNetZip from Powershell

The programming model is the same as for VB or C#, with a few syntactic changes for Powershell. This page will show some examples.


Create a zip file

This example just creates a simple zipfile

CopyCreate a Zip archive
1# Load the assembly - replace this with your own path
2[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");
3
4$directoryToZip = "c:\\temp";
5$zipfile =  new-object Ionic.Zip.ZipFile;
6$e= $zipfile.AddEntry("Readme.txt", "This is a zipfile created from within powershell.")
7$e= $zipfile.AddDirectory($directoryToZip, "home")
8$zipfile.Save("ZipFiles.ps1.out.zip");
9$zipfile.Dispose();

Create a zip file using AES encryption

This example creates a zipfile, using AES 256-bit encryption to encrypt the entries.

CopyCreate an encrypted zip
 1# Load the assembly - replace this with your own path
 2[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll")
 3
 4$directoryToZip = "c:\\dinoch\\dev\\powershell"
 5$zipfile =  new-object Ionic.Zip.ZipFile
 6$zipfile.Encryption = [Ionic.Zip.EncryptionAlgorithm]::WinZipAes256
 7$zipfile.Password = "Albatros$"
 8$e= $zipfile.AddEntry("Readme.txt", "This is a zipfile created from within powershell.")
 9$e= $zipfile.AddSelectedFiles("name != *.zip", $directoryToZip, "home")
10$zipfile.Save("ZipFiles.ps1.out.zip")
11$zipfile.Dispose()

Create a number of zip files, each containing a single file

This example iterates through a set of files, and creates a zipfile containing a single file, for each one.

CopyCreate a set of Zipfiles
 1# Load the assembly - replace this with your own path
 2[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");
 3
 4foreach ($file in gci $LogPath -filter $FileType -recurse |
 5        where{$_.LastWriteTime -lt [DateTime]::Now.AddDays($DaysOld)})
 6{
 7    $FileName = $File.FullName
 8    $ZipName = $File.FullName + ".zip"
 9    $zip = new-object Ionic.Zip.ZipFile
10    $zip.AddFile($FileName, "");
11    $zip.Save($ZipName)
12    $zip.Dispose()
13}