4

I'm using powershell to copy file to a remote computer witht he following command :

Copy-Item -Path [MyPath]\* -Destination \\[server]\[MyPath] -force

It's working great, but sometime I'm receiving the following error message: "The process cannot access the file [...] because it is being used by another process.".

Is it possible to force the file to be copy even if it's in use?

2 Answers2

1

The only way to do that is to get rid of the handle the process has to the file you are overwriting.

It's either a service or a desktop application accessing the file. You can find out what has access to the file using handle.exe from SysInternals.

Once you know what is accessing the file you can stop/kill it remotely (assuming you have permissions to do so).

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Thanks a lot, I'm already closing the windows service but the file stay lock... I will try this : http://stackoverflow.com/questions/958123/powershell-script-to-check-application-thats-locking-a-file – Marc-André Bilodeau-Lamontagne Apr 11 '13 at 17:26
0

I ran into this issue and wrote an entirely self contained script because I didn't want to depend on SysInternals. Script will identify and kill any process locking a file before making a full recursive copy.

https://github.com/Tikinsin/ForceCopy.ps1/blob/main/ForceCopy.ps1

Ryan Lutz
  • 355
  • 2
  • 8