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
casey-osman
Posts: 9
Last visit: Wed Jun 07, 2023 1:12 pm

Copy-Item copying folder and files but files are locked

Post by casey-osman »

I am using

Code: Select all

Copy-Item (Join-Path $source "*") -Destination $dest -Recurse
to copy a template folder structure that includes several files. I can run this from inside PS Studio just fine, but when I package it into a .exe, after the program runs, the files that were copied are saying they are in use. Essentially the file handles are still open.

This seems to only be an issue when the files are copied to a server location. When I copy them to my desktop, it works fine. I have full permissions in the folder that I am copying them into on the server.

Any suggestions?
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 »

As an update, it seems to be only related to when the project is packaged as a .exe. When I just export the project as a .ps1 it works fine. I would prefer to have this packaged as a .exe ideally - Mostly for the aesthetic reason of not having the PowerShell window flash on the screen as the form is loading.
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 »

I find this tool essential in tracking down who keeps a file open: https://lockhunter.com/
Maybe it helps you.
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 »

Alexander Riedel wrote: Mon Jan 16, 2023 9:05 am I find this tool essential in tracking down who keeps a file open: lockhunter
Maybe it helps you.
It is not that another user has a file open before I run the program.

When I run my script, it copies a folder tree that includes files. After the copy completes, ALL of the files that were copied are essentially still open from the copy command. The new (copied) file handle is remaining open after being copied.

Just after this portion of my script, I rename folders, which fails for any folder branch with a file in it since the files are "open".

Again, this only happens when the project is packaged as an .exe and the destination of the copied files is a server location.
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 »

Copy-Item does not leave files open. Even on a network drive and run from a packaged exe. I just tried.
Pause the app after the copy and check with lockhunter. Something has your files or folders open but it should not be copy-item. Be quick about checking as the lock might be brief.
In cases like this, it is very likely a malware scanner having your files open. A simple wait between the end of copying and renaming stuff will suffice in most cases.
The difference between a console script and a packaged app "might" be current working folder and most definitely timing.
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 »

Alexander,

The problem is, when I run the script from within PS studio it works fine. How would I pause the script while it is running as an EXE? Do you mean actually hard coding a Pause?

Also, the file handles remain open even after the script is completed. I have to go to the server and manually close all of the files through Computer Management.

I could see it being malware scanning, but the only thing running is windows defender on the server and my local computer. Also, I would think it would happen for both methods of running the script...via exported .ps1 and .exe since the files are being copied the same way, but that is not the case. The .ps1 works fine.

I appreciate your help on this.
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 »

Update

I tried Lockhunter and it says "No processes locking this file or folder have been found". But I still cannot open any of the files that were copied.

I also updated my script with a 20 second pause:

Code: Select all

function Copy-FolderStructure ($source, $dest, $jobNum)
{
	if (-not (Test-Path $dest))
	{
		New-Item -Path $dest -ItemType Directory
	}
	
	#Copy folder tree
	Copy-Item (Join-Path $source "*") -Destination $dest -Recurse
	Start-Sleep -Seconds 20
	$sourceTree = Get-ChildItem -Path $source -Recurse
The result is the same, when I try to open the file I get "The process cannot access the file because it is being used by another process". As mentioned before, the "lock" persists even after the script is complete and closed.

Is there a way to monitor file handles while they are being copied within a script?
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 »

Another update

I run this script using a right-click menu item (set via registry) to call the script that is saved on a network share. When the script runs it uses Get-Location to find the PWD which is the folder where the user right-clicks to select the script (this is the intended functionality). That location always shows as correct. The location of the script is on a different share than the location it is being called from (right-clicked).

I ran a test by moving the actual .exe into the folder I wanted the files copied to and ran the .exe directly from there (not by using the right click) and did not have the issue with the files remaining open.

When I run a right-click option that calls the .ps1, it works fine no matter the location of the actual .ps1

Is there some NTFS or AD limitation to running .exe files across shares? That seems to be the limiting factor. I am a domain admin and have full control of both locations, so I can't imagine it is just a normal NTFS permission issue.
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 »

.NET (which is the underlying architecture) does have a notion of secure location. But then it would not run at all. At least as far as I know.
You need to find out which process has your files open, otherwise it is just guesswork. LockHunter will tell you who has the files open.
Assuming your script terminates correctly and the packager process terminates as well, there is no way it can keep the files open.
All file handles owned by a process are closed when a process terminates.
Which PowerShell version is this anyway?

Yes, I meant hard coding a "Press any key to continue" message box or whatever you want to do so for the sake of finding out you have a defined spot where you can check who has your files open.
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 »

Alexander Riedel wrote: Mon Jan 16, 2023 4:12 pm LockHunter will tell you who has the files open.
LockHunter returned "No processes locking this file or folder have been found"

Computer Management on the server shows the files being open, but 0 locks. The files are open in Write + Read mode and Accessed By is listed as my user since I am the one that ran the script. No one else would have these files open since they are just a template folder structure.
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