Expand-Archive in powershell is failing to extract nested folders and files Expand-Archive in powershell is failing to extract nested folders and files powershell powershell

Expand-Archive in powershell is failing to extract nested folders and files


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 variableforeach($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?


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.


Adding -force to the command worked for me.