Skip to content

Instantly share code, notes, and snippets.

@marcoandre1
Last active May 19, 2024 09:16
Show Gist options
  • Save marcoandre1/2910b0ebc8503fdd3bf972f65834727d to your computer and use it in GitHub Desktop.
Save marcoandre1/2910b0ebc8503fdd3bf972f65834727d to your computer and use it in GitHub Desktop.
7-zip in powershell with scripts provided

Discovering 7zip use in powershell

If you need more info

7-Zip and unzipping from command line
e (Extract) command
x (Extract with full paths) command
-o (set Output directory) switch

How to extract .zip files

7-zip can be used in powershell for extracting .zip file. To call 7-zip simply use:

> 7z
# or
> 7z --help

To extract the files in the same folder where the .zip file is located, use the e (Extract) command. This will not keep the subfolders from the .zip file:

> 7z e folder.zip
# or
> 7z e 'path\to\folder.zip'

If you want to keep the files in their respective subfolders, you need to use the x (Extract with full paths) command like this:

> 7z x folder.zip
# or
> 7z x 'path\to\folder.zip'

If you want to extract the files to a specified folder, use the -o (set Output directory) switch. Keep in mind that there is no space between the switch -o and the destination folder. If that folder does not exist, it will be created automatically:

> 7z x files.zip -ofolder
# or
> 7z x 'path\to\folder.zip' -ofolder
# You can also use e instead of x if you don't want to keep the subfolders, only the files.

Powershell script to extract .zip files

If you need more info

How to handle command-line arguments in PowerShell
PowerShell Parameters
How do you execute an arbitrary native command from a string?
Invoke-Expression
Powershell Remove all text after last instance of \

Script

7zipExtract.ps1

param (
[string]$filePath = $(throw "The -filePath argument is required"),
[string]$folder = $( Read-Host "Please, specify the destination folder name" )
)
$path = $filePath.Substring(0, $filePath.lastIndexOf('\'))
$command = "7z x '$filePath' -o'$path\$folder'"
Invoke-Expression $command
@trboomer
Copy link

It's not really PowerShell, it's PowerShell running a 7zip executable. I've been doing that for years. Not really what I am looking for.

@marcoandre1
Copy link
Author

Hello @trboomer! Sorry to hear that this is not what you were looking for.
Yes, you're right. This is PowerShell running a 7zip executable.
At the time of making this gist I wanted to extract a zip folder from the command line. What were your trying to do?

@10bn
Copy link

10bn commented Oct 21, 2021

Is there also a way of extracting and moving the archive to trash?

I currently got this but couldn't figure out how to move the archive to the trash afterwards.

7z x *.zip -o* -aoa

@MostHated
Copy link

@jaddel

$command = "7z x '$filePath' -o'$path\$folder'"
Invoke-Expression $command
Remove-Item -Path $filePath -Confirm:$false -Force | Out-Null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment