Compress-Archive Error: Cannot access the file because it is being used by another process

Question:

I would like to zip a path (with a service windows running inside).

When the service is stopped, it works perfectly, when the service is running, I have the exception:

The process cannot access the file because it is being used by another
process.

However, when I zip with 7-zip, I don’t have any exception.

My command:

Do you have any idea to perform the task without this exception?

Answer:

Copy-Item allows you to access files that are being used in another process.

This is the solution I ended up using in my code:

The idea is that you pass through all the copied items through the pipeline instead of having to copy them to a specific destination first before compressing.

I like to zip up a folder’s content rather than the folder itself, therefore I’m using Get-ChildItem before compressing in the last line.

Sub-folders are already included. No need to use -recurse in the first line to do this

Source:

Compress-Archive Error: Cannot access the file because it is being used by another process by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply