Overview
- Using
Expand-Archive
to unzip files - Using .Net class
System.IO.Compression.ZipFile
to extract compressed file contents to a folder - PowerShell script
PowerShell Microsoft.PowerShell.Archive module
contains two cmdlets that let you to create zip and extract or unzip archive file to a folder.
These cmdlets are :
Compress-Archive
: cmdlet let you to compress or zipped the file from specified files or directories.
Expand-Archive
: cmdlet let you to extract or unzip files from specified archive file to a specified destination directory.
Read more about to download zip file from url using PowerShell.
The following steps describe how to unzip a file to folder
- Using Expand-Archive cmdlet
Expand-Archive -LiteralPath 'D:\PowerShell\archive[v1].Zip' -DestinationPath D:\PowerShell\UnzippedFolder
Above command extract the archive[v1].zip file contents to a specified destination path parameter D:\PowerShell\UnzippedFolder
PowerShell Expand-Archive cmdlet unzip a file content. In the above command, we have used LiteralPath as zip file contains wildcard characters in file name.
In the below example, we will use Expand-Archive cmdlet with -Path parameter for zip file location and -DestinationPath parameter to extract zipped file content
Expand-Archive -Path 'D:\PowerShell\archive.Zip' -DestinationPath D:\PowerShell\UnzippedFolder
- Using .Net class System.IO.Compression.ZipFile
.Net Framework 4.5 introduces ZipFile
class to manage archive files. ZipFile
class is available under System.IO.Compression
namespace. ZipFile
class provides static methods to create,extract and open archive files.
Tip: To use ZipFile class, add reference of System.IO.Compression.Filesystem
assembly in your script.
Below 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')
We have add assembly System.IO.Compression.Filesystem
reference. ZipFile
class provide ExtractToDirectory
method accepts source archive file name path and destination path to extract zip file contents.
PowerShell Script
# 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
Above article help you about how to unzip file in a specified folder using multiple ways in PowerShell script.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.