Copy-Item copying folder and files but files are locked

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Copy-Item copying folder and files but files are locked

Post by Alexander Riedel »

Oh goody a puzzle :D
So what is the PowerShell version? This is all a little strange. I have never heard of files staying open once the process that opened them terminates.
Are you certain the process terminates?
Alexander Riedel
SAPIEN Technologies, Inc.
casey-osman
Posts: 9
Last visit: Wed Jun 07, 2023 1:12 pm

Re: Copy-Item copying folder and files but files are locked

Post by casey-osman »

PowerShell version installed on my machine is: 5.1.19041.1682

The Copy-Item command seems to finish since the rest of the script is run successfully (other than not renaming the folders that have files in them). It seems that Copy-Item is a blocking command so it would have to finish before the rest of the script is run, correct?

The form window is closed after all of the script functions are complete so that should close all related processes, right?

When I check running processes in Task Manager I don't see anything that seems to be related to my script. Unless it could be called something unrelated.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Copy-Item copying folder and files but files are locked

Post by jvierra »

If the share is part of a replication set then this can happen on busy networks.

I would start by restarting the server as things can get out of sync. The network techs should be advised as to this condition as they would know more about the configuration of the servers and any replication that may be defined.

You can temporarily turn on detailed file logging to see what is causing the locks.
casey-osman
Posts: 9
Last visit: Wed Jun 07, 2023 1:12 pm

Re: Copy-Item copying folder and files but files are locked

Post by casey-osman »

jvierra wrote: Mon Jan 16, 2023 6:52 pm If the share is part of a replication set then this can happen on busy networks.
Thanks for the recommendation, and I thought about that as well.
We do use DFS replication (I set up the severs), but that doesn't explain why it only happens when running the script from the .exe and not the .ps1. I have also tested it on a server with no replication and the outcome is the same -- the .ps1 version works fine, but the .exe results in open files.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Copy-Item copying folder and files but files are locked

Post by jvierra »

Logging open handles for the folder and files would be the best way to proceed. This will tell you what process has opened and locked the files.
Terryser
Posts: 1
Last visit: Wed Feb 15, 2023 1:47 am

Re: Copy-Item copying folder and files but files are locked

Post by Terryser »

casey-osman wrote: Sun Jan 15, 2023 11:27 pm Any suggestions?
My suggestion is to use any of the sync/copy tools that have the ability to copy open/locked files/ in-use files/real-time files like Goodsync and Gs Richcopy360 , both are easy and quick
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked