How to unzip a file in Powershell?

前端 未结 9 2156
清歌不尽
清歌不尽 2020-11-29 17:07

I have a .zip file and need to unpack its entire content using Powershell. I\'m doing this but it doesn\'t seem to work:

$shell = New-Object -Co         


        
9条回答
  •  被撕碎了的回忆
    2020-11-29 17:16

    Here is a simple way using ExtractToDirectory from System.IO.Compression.ZipFile:

    Add-Type -AssemblyName System.IO.Compression.FileSystem
    function Unzip
    {
        param([string]$zipfile, [string]$outpath)
    
        [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
    }
    
    Unzip "C:\a.zip" "C:\a"
    

    Note that if the target folder doesn't exist, ExtractToDirectory will create it. Other caveats:

    • Existing files will not be overwritten and instead trigger an IOException.
    • This method requires at least .NET Framework 4.5, available for Windows Vista and newer.
    • Relative paths are not resolved based on the current working directory, see Why don't .NET objects in PowerShell use the current directory?

    See also:

    • How to Compress and Extract files (Microsoft Docs)

提交回复
热议问题