unzip file using 7z in powershell

前端 未结 4 2040
长情又很酷
长情又很酷 2020-12-20 18:02

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 -         


        
相关标签:
4条回答
  • 2020-12-20 18:25

    Aliases are not meant for that, they are meant to proxy other commands, not commands + parameters.

    To achieve what you want you would need to use a functions:

    function sz($args) {
        Start-Process "$env:ProgramFiles\7-Zip\7z.exe" -ArgumentList $args
    }
    
    0 讨论(0)
  • 2020-12-20 18:29

    With 7zip PowerShell module, not 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'
    
    0 讨论(0)
  • 2020-12-20 18:43

    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!

    0 讨论(0)
  • 2020-12-20 18:45

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

    0 讨论(0)
提交回复
热议问题