CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – How to unzip files?

In our previous article, we have discussed about zip / compress the selected files or directories using PowerShell Compress-Archive cmdlet. Through this article, we are going to discuss how to unzip the zipped files.

Expand-Archive cmdlet – Unzip files from Archived file(s)

PowerShell provides Expand-Archive cmdlet to unzip the zipped files. Through its -Path argument we need to provide the zipped file path. This command allows to unzip multiple archives. Each zipped file path should be separated by a comma symbol (“,”).

We also need to specify the location where to place the extracted files through its -DestinationPath argument. For example, below command extract the files to the current directory.

PS C:\PowerShell> Expand-Archive -Path ".\ZippedFile.zip" -DestinationPath "."

If the command is successfully executed, you can see the extracted files in the destination path. Otherwise, it will through an error with error details. For example, if the duplicate files are in the destination path, it will throw an error; like below.

ExpandArchiveHelper : Failed to create file 'C:\PowerShell\SourceFile.txt' while expanding the archive file 
'C:\PowerShell\ArchivedFile.zip' contents as the file 'C:\PowerShell\SourceFile.txt' already exists. 
Use the -Force parameter if you want to overwrite the existing directory 'C:\PowerShell\SourceFile.txt' 
contents when expanding the archive file.
At C:\windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:397 char:17
+ ...             ExpandArchiveHelper $resolvedSourcePaths $resolvedDestina ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (C:\PowerShell\SourceFile.txt:String) [Write-Error], IOException
    + FullyQualifiedErrorId : ExpandArchiveFileExists,ExpandArchiveHelper

During the extraction, if duplicate files are exist in the destination path, we can instruct the command to overwrite the existing files using its parameter, -Force.

Using pipe symbol with Expand-Archive cmdlet

We can also pass inputs to Expand-Archive cmdlet, using pipe symbol. This helps to combine this command with other commands. Other commands’ output will be an input to this command.

For example, below command expands all the zipped files which will return from Get-ChildItem cmdlet, to the mentioned destination path.

PS C:\PowerShell> Get-ChildItem -Path ".\*.zip" | Expand-Archive -DestinationPath ".\SubDirectory"

Notice that, there is no -Path argument mentioned in Expand-Archive command. The inputs will be taken from Get-ChildItem results.

[..] David

PowerShell – How to unzip files?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top