Search code examples
powershell7zipwinrar

How do i run 7zip through powershell to extract a .rar folder?


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.


Solution

  • 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.'
        }
    }