Home » PowerShell » How to Unzip All Files in a Folder using PowerShell?

How to Unzip All Files in a Folder using PowerShell?

Overview

PowerShell Microsoft.PowerShell.Archive module contains two cmdlets that let you create a zip and extract or unzip archive all files in a folder.

These cmdlets are :

Compress-Archive : cmdlet lets you compress or zip the file from specified files or directories.

Expand-Archive : cmdlet lets you extract or unzip files from a specified archive file to a specified destination directory.

In this article, we will discuss how to unzip all files in a folder with PowerShell.

There are different ways to unzip files in a folder as given below.

  • Using Expand-Archive to unzip files
  • Using .Net class System.IO.Compression.ZipFile to extract compressed file contents to a folder
  • PowerShell script

How to use Expand-Archive to Unzip All Files in a Folder

The following steps describe how to unzip a file to a folder using the Expand-Archive cmdlet in PowerShell.

Expand-Archive -LiteralPath 'D:\PowerShell\archive[v1].Zip' -DestinationPath D:\PowerShell\UnzippedFolder

This command extracts the archive[v1].zip file contents to a specified destination path parameter D:\PowerShell\UnzippedFolder.

PowerShell Expand-Archive cmdlet unzip a file content. This command uses the -LiteralPath parameter to specify the zip file location containing wildcard characters in the file name and unzip all files in a folder specified by the -Destination parameter.

In the following example, the Expand-Archive command uses -Path parameter for zip file location and the -DestinationPath parameter to extract zipped file contents to a folder.

Expand-Archive -Path 'D:\PowerShell\archive.Zip' -DestinationPath D:\PowerShell\UnzippedFolder

Read more about how to download zip files from url using PowerShell.

Using ZipFile Class to Unzip Files in a Folder

.Net Framework 4.5 introduces ZipFile class to manage archive files. The ZipFile class is available under System.IO.Compression namespace. The ZipFile class provides static methods to create,extract and open archive files.

Tip: To use ZipFile class, add a reference of System.IO.Compression.Filesystem assembly in your script.

The following example shows how to extract zip file contents to a folder.

Add-Type -Assembly "System.IO.Compression.Filesystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory('D:\PowerShell\vscode-docker-master.zip','D:\PowerShell\Zip\UnZipFiles')

In the above PowerShell script, we have added assembly System.IO.Compression.Filesystem reference. The ZipFile class provides the ExtractToDirectory() method that accepts the source archive file name path and destination path to extract zip file contents.

PowerShell Script To Unzip All Files in a Folder

# Using Expand-Archive and LiteralPath parameter

Expand-Archive -LiteralPath 'D:\PowerShell\archive[v1].Zip' -DestinationPath D:\PowerShell\UnzippedFolder

#Using Expand-Archive and Path parameter

Expand-Archive -Path 'D:\PowerShell\archive.Zip' -DestinationPath D:\PowerShell\UnzippedFolder

#Using .Net class System.IO.Compression.ZipFile

Add-Type -Assembly "System.IO.Compression.Filesystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory('D:\PowerShell\vscode-docker-master.zip','D:\PowerShell\Zip\UnZipFiles')

Conclusion

I hope the above article on how to unzip file in a folder using multiple ways in PowerShell script is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.