Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unzip file using 7z in powershell

What is the command to unzip a file using 7z in powershell?

set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
sz x  $zipfilePath $destinationUnzipPath -aoa -r;

The command works fine but it says no files to process, everything is Ok rather than unzipping the file?

like image 935
Deepanshu Kalra Avatar asked Mar 24 '17 11:03

Deepanshu Kalra


People also ask

How do I unzip a zip file in PowerShell?

To unzip a file using PowerShell, you can utilize the “Expand-Archive” command. The Expand-Archive command unzips or extracts the content of a zipped or archived file to its destination folder.

How do I run 7-Zip from command line?

To begin a session, open a terminal window. Invoke the version of 7Zip you are using by entering "7z" for P7Zip (7z.exe), or "7za" for 7Zip for Windows (7za.exe) to start either the P7-Zip or 7za application prior to entering commands.


2 Answers

With 7zip PowerShell module, now it is hassle free

#   Install 7zip module

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Set-PSRepository -Name 'PSGallery' -SourceLocation "https://www.powershellgallery.com/api/v2" -InstallationPolicy Trusted
Install-Module -Name 7Zip4PowerShell -Force

#   Extract 7zip file

$sourcefile = "c:\source\sample.7z"
Expand-7Zip -ArchiveFileName $sourcefile -TargetPath 'c:\destinaation'
like image 152
Prosenjit Sen Avatar answered Nov 12 '22 04:11

Prosenjit Sen


I didn't want to use aliases, functions or Start-Process. After a little looking around the web, I've found this gem (and I can't remember where):

& ${env:ProgramFiles}\7-Zip\7z.exe x $zipfilePath "-o$($destinationUnzipPath)" -y

And you can add a > $null at the end if you don't want to see 7z's messages!

like image 25
João Ciocca Avatar answered Nov 12 '22 03:11

João Ciocca