Compress-Archive with hidden items

I’m trying to zip a folder that contains some hidden files and folders. I noticed that Compress-Archive does not grab these so I thought I’d simply pass all the items.

Get-ChildItem C:\temp\test\* -Force |
    Compress-Archive -DestinationPath c:\temp\tests1.zip -CompressionLevel NoCompression

This is met with a nasty error. It looks like Get-Item is failing to query the hidden files.

Get-Item : Could not find item C:\temp\test\New Text Document.txt.
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:814
char:63
+ ... Entry.LastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\test\New Text Document.txt:String) [Get-Item], IOException
+ FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetItemCommand

Exception setting "LastWriteTime": "Cannot convert null to type "System.DateTimeOffset"."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:814
char:25
+ ... $currentArchiveEntry.LastWriteTime = (Get-Item -LiteralPa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting

Testing confirmed Get-Item would also need the -Force parameter to query it. Is there a work around I’m not finding? Or should I just use 7zip or similar? I do not want to change the attributes just to zip them. If I zip them in Windows Explorer it zips them all just fine.
I appreciate any input.

1 Like

I could reproduce the error. You could workaround with using a Foreach-Object loop and the parameter -Update but that would produce another weird error. I’d probably rather use 7zip instead. :wink:

This works using .NET but the hidden attribute isn’t preserved (if that doesn’t matter once zipped):

$container = 'C:\Users\user\Desktop\test'
$zipPath.  = 'C:\Users\user\Desktop\test.zip'
[System.IO.Compression.ZipFile]::CreateFromDirectory($container, $zipPath)

There isn’t an overload for overwriting existing files but there are plenty of ways to handle that beforehand.

https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile.createfromdirectory?view=netcore-3.1