I have 5 zip files and 5 folders respectively. Files are: FranceData.zip, USAdata.zip, GermanData.zip, Italydata.zip and DanemarkData.zip. Folders are: FranceFolder, USAFolder, GermanFolder, ItalyFolder and DanemarkFolder. I would like to know how would I unzip these files into their respective folders using Array in Powershell. I am knew to Powershell and need help. Thank you
1 Answers
This can be done using the Expand-Archive
cmdlet, which is available in PowerShell 5 and above. (PowerShell's version can be checked via the $PSVersionTable
variable.)
To extract a zip file to a specific folder the following syntax is used:
Expand-Archive -Path 'ZipFile.zip' -DestinationPath 'ZipFolder'
or
Expand-Archive -LiteralPath 'ZipFile.zip' -DestinationPath 'ZipFolder'
If the -Path
parameter is used, PowerShell will recognise characters such as *
, ?
, [
, ]
, as wildcards, which can cause unexpected behaviour for file-paths that contain square-brackets.
If the -LiteralPath
parameter is used, PowerShell will not treat any characters as wildcard characters.
Assuming all your zip files and folders follow the same naming pattern you could use an array like so:
$Countries = @(
'France',
'USA',
'German'
'Italy',
'Danemark'
)
foreach ($Country in $Countries)
{
$ZipFilePath = $Country + 'Data.zip'
$DestinationPath = $Country + 'Folder'
Expand-Archive -LiteralPath $ZipFilePath -DestinationPath $DestinationPath
}
If your files and folders don't follow the same naming pattern you could use a dictionary (or a collection of KeyValuePair
s), like so:
$ZipFilesAndFolders = @{
'FranceData.zip' = 'FranceFolder'
'USAData.zip' = 'USAFolder'
'GermanData.zip' = 'GermanFolder'
'ItalyData.zip' = 'ItalyFolder'
'DanemarkData.zip' = 'DanemarkFolder'
}
foreach ($KeyAndValue in $ZipFilesAndFolders.GetEnumerator())
{
$ZipFilePath = $KeyAndValue.Key
$DestinationPath = $KeyAndValue.Value
Expand-Archive -LiteralPath $ZipFilePath -DestinationPath $DestinationPath
}
With PowerShell 4 (and 3)
If you have .Net Framework 4.5 installed, you can use Microsoft.PowerShell.Archive
which was created by the PowerShell team.
PowerShell 4 requires .Net Framework 4.5 so this should work without any system changes needed.
The module can be found at https://github.com/PowerShell/Microsoft.PowerShell.Archive
The syntax used is identical, the function is Expand-Archive
and the parameters are -Path
, -LiteralPath
, and -DestinationPath
.
Just ensure that you have imported the module before using it, this can be done using the Import-Module
cmdlet like so:
Import-Module -Name 'Microsoft.PowerShell.Archive' -Force
PowerShell 2
A solution for PowerShell 2 can be found here: https://stackoverflow.com/a/37814462/9447234

- 72
- 2
- 8
-
Thank you very much. I will try with the last example which is what I am looking for and gets back o you. Thanks again. I really appreciate it. – serob Sep 30 '18 at 16:09
-
I checked the version and I have Version 4.0. Do you think it will work on 4.0? If not could you please tell me how in Version 4.0. Thank you – serob Sep 30 '18 at 17:18
-
I tried the code and Expand-archive is not a recognise CMDLET in version 4.0. – serob Sep 30 '18 at 17:33
-
I have given 5 files and 5 folders as an examples but in fact I will be receiving 40 zip files everyday that needs to be extracted in theirs folders. The examples you gave me is working fine in PS5 but I have PS4 version and I cannot upgrade to PS5 because I don't have the permission to do so. Please could you help me out. Thank you – serob Sep 30 '18 at 20:27
-
Thank you very much Harry. I will try it and Let you know shortly. – serob Oct 01 '18 at 21:55
-
After downloading the module, should I save it in the same folder of the scritp? – serob Oct 01 '18 at 21:58
-
You can save it wherever, you can use the path of the module's manifest, which in this case is the manifest is `Microsoft.PowerShell.Archive.psd1`. Saving it in the same folder as the script is likely easiest. – Just-Harry Oct 01 '18 at 22:46
-
I notice that I don't have permission to download nor to import anything to prod server. So I am limited to develop the project in PS4. I come up with a solution that works without importing modules: here is the code and it worked. I am suspecious about the performance though. – serob Oct 02 '18 at 18:54
-
$ZipFilesAndFolders = @{ 'FranceData.zip' = 'FranceFolder' 'USAData.zip' = 'USAFolder' 'GermanData.zip' = 'GermanFolder' 'ItalyData.zip' = 'ItalyFolder' 'DanemarkData.zip' = 'DanemarkFolder' } [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null foreach ($KeyAndValue in $ZipFilesAndFolders.GetEnumerator()) { $ZipFilePath = $KeyAndValue.Key $DestinationPath = $KeyAndValue.Value [System.IO.Compression.ZipFile]::ExtractToDirectory($ZipFilePath, $DestinationPath) } – serob Oct 02 '18 at 18:55
-
Thank you very much Harry for your help on this. One last thing I would like to ask. Knowing that I will be receiving zip files everyday, I want to overwrite the existing extract files with the new extract files. I do not want to remove the files but just overwrite using the same code that I posted it earlier on. Thank you – serob Oct 03 '18 at 00:42