,

powershell_logo At work we deal with different big databases, and by big I mean between 3.5 and 8 GB. Those databases are zipped with 7-Zip and stored on a server. Depending on the scenario we unzip one of these databases to a local folder and attach it to the SQL Server. To simplify these steps we have a couple of Powershell scripts, among other things invoking the command line version of 7-Zip.

However, extracting an archive of several gigabytes over the network might take some time, and we’d like to see the progress in the Powershell console. Powershell provides a standard way to report progress by calling Cmdlet.WriteProgress, which then will be displayed by the current host (e.g. command line) appropriately.

Therefore with some support of a co-worker I’ve written two Cmdlets for extracting and compressing archives. The syntax is simple as this:

Expand-7Zip
    [-ArchiveFileName] <string>
    [-TargetPath] <string>
    [<CommonParameters>]

Compress-7Zip 
    [-ArchiveFileName] <string> 
    [-Path] <string>
    [[-Filter] <string>]
    [-Format <OutArchiveFormat> {SevenZip | Zip | GZip | BZip2 | Tar | XZ}]
    [-CompressionLevel <CompressionLevel> {None | Fast | Low | Normal | High | Ultra}]
    [-CompressionMethod <CompressionMethod> {Copy | Deflate | Deflate64 | BZip2 | Lzma | Lzma2 | Ppmd | Default}]
    [<CommonParameters>]

It works with both x86 and x64 and uses SevenZipSharp as a wrapper around 7zip’s API.

7zip4powershell

You can download the cmdlets with the required libraries here or grap the sources at Github.

Comments

Dave

Hi Thomas,

Thanks for great work. I have downloaded the package and started using. It’s really great.
Can you please help me how can I modify your package ?
Actually I want to exclude few folders and file extensions, but filter parameter just include those files/folder which are filtered.
May be your work required that, but is there any existing setting to exclude folders and files?

Thanks,
Dave

Thomas Freudenberg

Hi Dave,

I didn’t need the exclusion of files and folders, so I didn’t implement it. Feel free to modify my code, you can find it on Github. How about using the pipeline featire of PowerShell? I would accept a pull request.

Best regards
Thomas

Mathieu

Nice.

Import-Module -Name “7Zip”

Works when all the libraries from 7Zip4PowerShell.zip are placed in the modules directory, and create a module manifest data file (e.g. c:\Windows\System32\WindowsPowerShell\v1.0\Modules\7Zip\7Zip.psd1):

  
@{  
 GUID = '{00000000-1111-2222-3333-000000000000}'  
 Author = 'Thomas Freudenberg'  
 CompanyName = 'N/A'  
 Copyright = '© Thomas Freudenberg'  
 ModuleVersion = '1.0.0.0'  
 PowerShellVersion = '2.0'  
 HelpInfoUri = "[http://thomasfreudenberg.co...](http://thomasfreudenberg.com/archive/2013/04/07/7-zip-for-powershell "http://thomasfreudenberg.com/archive/2013/04/07/7-zip-for-powershell")"

 NestedModules = @(  
 "7Zip4Powershell.dll")  
 CmdletsToExport = @(  
 "Expand-7Zip",  
 "Compress-7Zip"  
 )  
}

Zack Price

Before extracting the zip contents, I needed to right click -> properties -> unblock.
Otherwise, the module wouldn’t install because it was blocked.

Aaron GrandeCorazón Clark

Did he ever implement this feature? I need it for a script at work and I am willing to put in the effort if he hasn’t already.

sotropino

Great job.

Even greater if it had two additional cmdlets to mimic the useful “l” and “t” switches available in 7z.exe:

  • Get-7ZipContents ? (list contents of archive)
  • Test-7Zip ? (test integrity of archive)

Any .NET-savvy challenged to help?

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...