#PSTip Compress files and folders with System.IO.Compression.FileSystem class

Using the CreateFromDirectory and ExtractToDirectory methods, it is possible to compress and extract files. In this tip I will show different constructors that can be used to either compress or extract files using this class. The following example will compress the files stored in the c:\testing folder:

Add-Type -Assembly 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory('c:\testing', 'c:\testing.zip','Optimal',$false)

When you want to extract files, use the ExtractToDirectory method:

[System.IO.Compression.ZipFile]::ExtractToDirectory('c:\testing.zip', 'c:\newtest')

In PowerShell 5.0 the Compress-Archive and Expand-Archive cmdlets are available to simplify working with compressed archives.

For more information about the ZipFile class and the Compress-Archive and Expand-Archive cmdlets please refer to the following articles:

ZipFile Class



About the author: Jaap Brasser

Jaap is a Senior System Engineer in the Financial Services industry. He focuses on Microsoft and related technologies and has a passion for PowerShell. In his spare time he maintains his Blog, Twitter feed, @Jaap_Brasser, and contributes on the Official Scripting Guys Forum and Windows PowerShell TechNet forums

Related Posts