PowerShell Unzip

Migel Hewage Nimesha Dec 24, 2021
  1. Expand-Archive Syntax 1
  2. Expand-Archive Syntax 2
  3. Expand-Archive Parameters
  4. PowerShell Unzip Files Examples
PowerShell Unzip

The ZIP file type minimizes its size by compressing files to a single file. This method saves space on the hard drive, encrypts information, and makes it simple to exchange files with one another.

PowerShell extracts files and folders from a specific Zip file on the same directory where the ZIP file resides or on the specified destination path using the Expand-Archive command.

Here we can use two different syntaxes for deletion. These two can be used separately but not combined.

Expand-Archive Syntax 1

Here path property is used, and wildcards are possible when giving the path.

[-Path] <String>
[[-DestinationPath] <String>]

Expand-Archive Syntax 2

The next option is the literal path. Here, the path given should be accurate since it is taken as given, and wild card use isn’t allowed. If the pathway contains escape characters, surround each escape character inside single quote marks to tell PowerShell that no characters should be interpreted as escape patterns.

-LiteralPath <String>
[[-DestinationPath] <String>]

Expand-Archive Parameters

The name and the description of the most common parameters used are given below.

Name Description
-Confirm confirmation is prompted before running cmdlet
-DestinationZip Specify the path to the unzip folder. If the folder doesn’t exist, the target folder is created
-Force force cmdlet without confirmation
-LiteralPath Give path to the archive file. Literal path value is taken exactly as its given.
-PassThru Output a list of files which is expanded from archive
-Path specify the path to archive file
-WhatIf show what happens if the cmdlet is run without actually running it

PowerShell Unzip Files Examples

Example 1:

The items of a current archive file in the current directory are extracted into the folder supplied by the -DestinationPath option in this example.

Expand-Archive -Path test.Zip -DestinationPath C:\Test

Example 2:

It’s even easier to extract files from an archive than to create one. All you have to do is give the archive’s name and the unzipped files’ destination location. Using the Expand-Archive cmdlet, the code below exports the content of the test zip archive to a specified folder TestUnzipped in the destination path variable.

Expand-Archive -LiteralPath 'C:\Archives\test[v1].Zip' -DestinationPath C:\TestUnzipped

Since the filename comprises characters that may be construed as wildcards, the LiteralPath option is utilized.

If you don’t use the -DestinationPath argument, PowerShell will unpack the files into the current root folder and build a new folder with the filename of the Zip file.

When PowerShell attempts to extract the files, this will throw an error if the directory now exists in the destination. Using the -Force argument, you may push PowerShell to replace the old data with new data.

The -Force argument should only be used if the old files are really no longer required, as it will permanently overwrite the files on your machine.

The ZIP file compresses and stores files and directories into a single document. Without having to install any software, PowerShell can retrieve them in various methods. The approach outlined above are applicable to both cross-platform and native apps.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.