-1

i have googled this already but i simply cannot find a straightforward right answer. i want to add a way to extract a .rar folder whether it be via winRAR or 7zip into my powershell script so i can automate it. I know powershell has a built in function for zip files, but unfortunately my boss wont use zip because it doesn't compress things as well as .rar.

Dpw808
  • 119
  • 1
  • 3
  • 10
  • [Does this answer on SuperUser help?](http://superuser.com/questions/458643/unzip-rar-from-command-line-with-7-zip) – G42 Nov 20 '15 at 14:42
  • yes, though i forgot to mention i have to extract it TO a certain place, and what you linked does not tell me how to put the destination unless its like simple batch where you just put the destination path right after the file location path and it just assumes. that was my fault for not specifying that sorry. – Dpw808 Nov 20 '15 at 16:11
  • [Add `-o` to the end.](http://superuser.com/questions/95902/7-zip-and-unzipping-from-command-line) Use parenthesis if your path has spaces. – G42 Nov 20 '15 at 16:30
  • ah great thanks, but isnt this answer in batch? cant i not add batch to my powershell script? – Dpw808 Nov 20 '15 at 16:35
  • The best way to find out is to try it. Let me know if you have any questions after that. – G42 Nov 20 '15 at 16:59

2 Answers2

3

I believe 'e' is for extract (there is also x, so adjust as needed):

$SourceFile="blahblah.zip"
$Destination="C:\temp\myfolder"
&$zipExe x $SourceFile "-o$Destination" -y 

should work. $zipExe would be your path to the 7zip. For example:

$zipExe = join-path ${env:ProgramFiles(x86)} '7-zip\7z.exe'
if (-not (test-path $zipExe)) {
    $zipExe = join-path ${env:ProgramW6432} '7-zip\7z.exe'
    if (-not (test-path $zipExe)) {
         '7-zip does not exist on this system.'
    }
}
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
  • thanks i will try this, i cant use the batch answer because i dont think you can just put a patch command into a powershell script? – Dpw808 Nov 20 '15 at 16:19
  • also i want to verify, $outfile is the destination for the extracted file? $path is where the file is before its extracted? and zipargs? – Dpw808 Nov 20 '15 at 16:26
  • awesome its working now! thanks! one other question, i want to run this script on any computer and i found the [environment]::UserName to automatically put in that name into the path on the desktop and i put it in a variable, but i dont know how to call the variable correctly so that it inputs it into the path of the zip location. – Dpw808 Nov 20 '15 at 18:38
  • Not clear on the question. If you create a wuestion with your code and explain the problem, it may be easier to help – Adil Hindistan Nov 20 '15 at 22:34
1
7z e archive.zip

Extracts all files from archive archive.zip to the current directory.

Here are the commands and here is a guide to the command line syntax

Jon Carlstedt
  • 2,317
  • 5
  • 22
  • 27
  • so im a bit confused, you both gave me different answers, i would go for the one you wrote jon because its drastically less code, but it does what the one adil suggested does? – Dpw808 Nov 20 '15 at 15:58