Skip to content

Batch extract zip files with PowerShell

G19th August 2015

CNo Comments

TPowerShell, Windows

Use this simple PowerShell script to batch unzip large numbers of files at once.

Various pieces of computerised scientific equipment that I use generate large volumes of zip files which I have to unzip manually. Rather that suffer this tedium, I prefer to employ a PowerShell script that will:

  • Open each zip file
  • Create a new directory based on the name of the zip file
  • Extract the contents of the zip file to the new directory
  • Repeat the process for all zip files in all subdirectories (optional)

The following instructions describe the process:

Install 7za

The PowerShell script relies upon a third-party script called 7-Zip, which can be downloaded for free from Sourceforge. Unzip the files and install 7za.exe to a known location on your Windows machine. Note the location.

Amend PowerShell script variables

There are two variables that need to be modified for the PowerShell script to run:

  • $path = The file path that contains the target zip files
  • $7za = The file path that points to 7za.exe

Amend the $path and $7za variables within the following code:

$path ="c:\path\to\your\files\*.zip" #location of zip files
$7za ="c:\path\to\7za.exe" #location of 7za 

$ZipFileList = Get-ChildItem -Path ($path) -Recurse;

foreach ($ZipFile in $ZipFileList) {
    mkdir -Path ('{0}\{1}' -f $ZipFile.Directory, $ZipFile.BaseName);
    $ArgumentList = 'x "{0}" -o"{1}\{2}\"' -f $ZipFile.FullName, $ZipFile.Directory, $ZipFile.BaseName;
    Start-Process -FilePath ($7za) -ArgumentList $ArgumentList -Wait;
}

Execute the script in PowerShell and the files will be unzipped.

Another option

If you don’t want PowerShell to conduct a recursive search within the target folder, delete -Recurse from the script.

Credit

This script is based on the work of Trevor Sullivan.

   

Comments:

No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.