unzip file using 7z in powershell
Asked Answered
B

6

17

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?

Believe answered 24/3, 2017 at 11:44 Comment(2)
With the benefit of hindsight: The only problem turned out to be the syntax of the 7z.exe command - the fact that 7z was invoked from PowerShell (via an alias) was incidental.Skald
Thanks I wanted to test a zip file. Your answer got me started. line1:sz t $zipfile line 2 echo $LASTEXITCODE #0 for success else 7zip.bugaco.com/7zip/MANUAL/exit_codes.htmDilks
B
12

This finally worked for me sz x -o$destinationUnzipPath $zipfilePath -r ;

Believe answered 27/3, 2017 at 10:45 Comment(0)
L
13

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!

Leucopenia answered 8/10, 2019 at 15:4 Comment(2)
Thanks, this helped me work around this known bug: github.com/PowerShell/Microsoft.PowerShell.Archive/issues/69Polenta
Thanks, this work on powershell terminal; but cannot run on YML script file.Der
B
12

This finally worked for me sz x -o$destinationUnzipPath $zipfilePath -r ;

Believe answered 27/3, 2017 at 10:45 Comment(0)
M
9

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'
Milore answered 25/5, 2020 at 3:41 Comment(1)
If someone needs a code that works like clockwork on windows 7, windows 8 & 8.1, windows 10, this is the right one to use ;) The .Net version from microsoft is unreliable on some older windows OS. By the way this is what chocolatey package manager (chocolatey.org) use !Reduced
B
2

This command works well. It extracts the zip file to the destination directory:

$DESTINATION_DIR="C:\my_destination"
& "${env:ProgramFiles}\7-Zip\7z.exe" x $ZIP_FILE "-o$($DESTINATION_DIR)" -aoa -r
Buckram answered 2/3 at 1:29 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Elroyels
Z
1

My use case was slightly different because I had multiple tar files in a directory that needed to be extracted. I'm sharing it because same command may also be used or slighly modified to be used:

here is the command that worked for me on Windows 10 via Powershell:

Notice: you need to change the paths below for your use case of course.

$srcFolderPathWithTar="C:\SomeFilder\has\multiple\tar\files"
$targ="C:\Users\your_user_name\Downloads\new" 

. "C:\Program Files\7-Zip\7z.exe"  x "-o$($targ)" "$srcFolderPathWithTar" -r -y;
Zoochore answered 14/9, 2022 at 21:13 Comment(0)
B
0

I used "fullname", which includes the path.
Also, I had to change my directory in PowerShell to the output directory of the extracted data, i.e. D:\temp

I refuse to believe that copying or extracting a bunch of files from disparate folders to a single location is a complicated task in this age.

$rars = (Get-ChildItem "D:\Path\To\folder" -Recurse *.rar).fullname
foreach ($rar in $rars) {& "C:\Program Files\7-Zip\7z.exe" e $rar}
Brod answered 3/9, 2021 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.