11

I have the following simple powershell to extract a zip folder (containing other folders and only log files) to a destination

$FolderPath = "C:\Temp\Whatever"

Expand-Archive -Path "$FolderPath\logs.zip" -DestinationPath "$FolderPath\logs"

Unfortunately this returns a whole bunch of errors like below....

Remove-Item : Cannot find path 'C:\Temp\Whatever\logs\1_Selenium SEPA-Test\Attempt1\1_Start VM's\Release\1_Initialize Agent.log' because it does not exist.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:410 char:46
+ ...                 $expandedItems | % { Remove-Item $_ -Force -Recurse }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Temp\Whateve...alize Agent.log:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find path 'C:\Temp\Whatever\logs\1_Selenium SEPA-Test\Attempt1\1_Start VM's\Release\1_Initialize Job.log' because it does not exist.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:410 char:46
+ ...                 $expandedItems | % { Remove-Item $_ -Force -Recurse }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Temp\Whateve...tialize Job.log:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

and loads of other errors that are similar

I can confirm that the file referenced in the first error C:\Temp\Whatever\logs\1_Selenium SEPA-Test\Attempt1\1_Start VM's\Release\1_Initialize Agent.log does exist in the zip folder at an equivalent location...

enter image description here

After the script concludes, I do a see an incomplete folder in the directory specified.

enter image description here

Whats going on here?

Thanks,

Konzy262
  • 2,747
  • 6
  • 42
  • 71
  • What if you just change the `-DestinationPath` to a different folder? – trebleCode Apr 30 '18 at 19:42
  • Doesn't help unfortunately – Konzy262 Apr 30 '18 at 21:03
  • 1
    Can you use [7z e](https://sevenzip.osdn.jp/chm/cmdline/commands/extract.htm)? There's a [powershell script](https://stackoverflow.com/questions/25287994/running-7-zip-from-within-a-powershell-script) for running it. – lloyd May 01 '18 at 02:32
  • Might be an issue with your archive: https://github.com/PowerShell/Microsoft.PowerShell.Archive/issues/12 This issue seems to have now been fixed, so you might want to update Powershell if you're running an older version. – henrycarteruk May 01 '18 at 09:27
  • I'm using powershell 5.1. I've managed to cobble together a solution using 7z e – Konzy262 May 02 '18 at 06:39

9 Answers9

6

I had issues with this module in the past and a colleague and I cobbled together the following

# This script was created to extract the contents of multiple ZIP files located in a directory
# structure. Each ZIP files is extracted within the folder it resides.

# File path
$filepath = Get-ChildItem -Path 'C:\Users\Luke\Desktop\ArchivedScripts\' -Filter *.zip -Recurse

# convert filepath to NameSpace object
$shell = new-object -com shell.application

# ForEach Loop processes each ZIP file located within the $filepath variable
foreach($file in $filepath)
{
    $zip = $shell.NameSpace($file.FullName)
    foreach($item in $zip.items())
    {
        $shell.Namespace($file.DirectoryName).copyhere($item)
    }
    Remove-Item $file.FullName
}

Perhaps this is of some use?

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • Great and working solution! A simplified function for one ZIP file can be found here: https://serverfault.com/a/201604 – R Yoda Aug 03 '21 at 16:00
  • I'd like a little more explanation. I'm guessing this is opening a Windows Shell session, and then using that to explore the zip contents and copy them out of the zip file (expanding them in the process). Is this handling the long filepath issue by relying on the Windows Shell support for long filepaths, or is it solving a different issue with Expand-Archive? – Tydaeus Oct 24 '22 at 16:49
6

Whats going on here?

Expand-Archive fails when trying to expand some files (in my case it was due to the path being too long) and tries to remove the files that it thinks it extracted (see https://github.com/PowerShell/Microsoft.PowerShell.Archive/blob/master/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1#L418), but Remove-Item can't find any files since they were not actually extracted.

Switching to PowerShell 7 fixed the long path issue for me.

Leon V
  • 541
  • 5
  • 12
2

Adding -force to the command worked for me.

abc
  • 21
  • 1
2

I had the same issue. This issue occurs in the case file paths to be exported are too long.

sao33
  • 21
  • 1
2

I got the same error and it worked for me when I removed the double quotes for the ZIP FILE PATH.

For example :

$FolderPath = "C:\Temp\Whatever"

Expand-Archive -Path $FolderPath\logs.zip -DestinationPath "$FolderPath\logs"

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Rajeshwar
  • 565
  • 1
  • 5
  • 13
  • Wow - this worked for me! I was getting the following error running this command Expand-Archive -LiteralPath "$downloadFile" -DestinationPath "$Destination" : "Exception calling "ExtractToFile" with "3" argument(s): "The archive entry was compressed using an unsupported compression method.".Message" - removed double quotes from "$downloadFile" and it worked! Thanks... – Tom Apr 16 '23 at 15:52
1

I had no problems with long paths - it was something else - not sure what. I am running Windows Server 2019 and the default powershell is v5.1 (you can check with $PSVersionTable).

I installed v7.1 alongside v5.1. I was then able to run powershell v5 with powershell command and v7.1 with pwsh command. Expand-Archive now works wihtout any problems.

GChuf
  • 1,135
  • 1
  • 17
  • 28
1

This is the ONLY way I could get Expand-Archive to work in 2016 Server (PS 5.1) Whn I tried to run the exact same command locally it failed utterly...

#copy_and_expandarchive.ps1
$ServerList = gc D:\batch\input\servers.txt
Foreach ($server in $ServerList){
if(Test-Connection $server -count 1 -quiet){
    if($server -like '#*')
    {
        continue
    }
    Invoke-Command $server -ScriptBlock {
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Expand-Archive -force -verbose –LiteralPath 'D:\install\logstash-7-17-3GIS-Prod.zip' –Destination 'd:\apps'
    }}}

WHY does this work? Because the x86 version of PowerShell is defaulted in this image as the command interpreter (the 64 Bit PS: C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe only got applied once Group Policy succeeded - I had to disable IPv6 to get that done and run gpupdate /target:computer using a saps -wait to ensure it worked)

Patrick Burwell
  • 129
  • 1
  • 12
0

I got the same error in another situation.
I'm trying to unzip a differently encoded Zip file from another computer.

Solution 1: Keeping the same encoding worked for me.
Setup: Setting-->Language&region-->Region-->Current system locale.
Notice: don't check Bata: Use Unicode UTF-8 for worldwide language support.

Solution 2:Use another tool like Powershell7 or 7z.exe.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 06 '22 at 07:56
0

FYI that another way to hit this issue is to supply an incorrect destination path e.g. something like this (yes I make this mistake!).

$variable1="c:\a\path\somewhere"
...
# Note the errant space before the last quote below!
$variable2="${variable1} "
...
Expand-Archive ... -DestinationPath "${variable2}"...

So my destination path now doesn't exist and you get the same symptoms as the OP was hitting.

Paul D Smith
  • 639
  • 5
  • 16