Uli Kunkel Uli Kunkel - 5 months ago 32
PowerShell Question

How to unzip a file in Powershell?

I have a

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

$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
foreach ($item in $zip.items()) {

What's wrong? The directory
is still empty.


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.

How to Compress and Extract files